Если камера всегда направлена на ваш объект, и объект находится в центре экрана, вы можете легко решить эту проблему здесь https://docs.unity3d.com/Manual/FrustumSizeAtDistance.html. Но в целом этот класс будет выполнять эту задачу. Во-первых, вам нужно рассмотреть виртуальную сферу вокруг вашего объекта, которая полностью включает ваш объект, и этот код предотвращает выход объекта из экрана.
public class PutObjectAlwaysInScreen : MonoBehaviour {
//Object Transform that should be in the screen
public Transform Target;
//Considering a sphere around your object according to its size
public float Radius;
void check()
{
//Finding a frustum plate that include object position point
//the vector from camera to frustum plate
Vector3 Vcenter = Camera.main.transform.forward;
//Vector between camera and object
Vector3 VcameraToObject = Target.position - Camera.main.transform.position;
//The angle between center of frustum and object postion
float CosTeta = Vector3.Dot(Vcenter,VcameraToObject)/(Vcenter.magnitude * VcameraToObject.magnitude);
//distance between camera and frustum
float distance = VcameraToObject.magnitude * CosTeta;
//Center of frustum plate
Vector3 Fcenter = Camera.main.transform.position + distance * (Vcenter/Vcenter.magnitude);
//Vector between center of frustum and object
Vector3 FrustumToObject = Target.position - Fcenter;
//Width And Height of the distance between center of frustum and object in the screen
float W = Vector3.Dot(FrustumToObject,Camera.main.transform.right);
float H = Vector3.Dot(FrustumToObject,Camera.main.transform.up);
//frustum Width and Height
float frustumHeight = 2.0f * distance * Mathf.Tan(Camera.main.fieldOfView * 0.5f * Mathf.Deg2Rad);
float frustumWidth = frustumHeight * Camera.main.aspect;
//Check if the object is out of the screen
if( ((Mathf.Abs(W)+Radius)>frustumWidth) || ((Mathf.Abs(H)+Radius)>frustumHeight) )
{
// Do Something
// You Can Avoid Camera from getting closer to the Object
//or With the below formulae adjust the fieldOfView
if((Mathf.Abs(W)+Radius)>frustumHeight)
Camera.main.fieldOfView = 2.0f * Mathf.Atan((Mathf.Abs(H)+Radius) * 0.5f / distance) * Mathf.Rad2Deg;
if((Mathf.Abs(W)+Radius)>frustumWidth)
Camera.main.fieldOfView = 2.0f * Mathf.Atan(((Mathf.Abs(W)+Radius)/Camera.main.aspect) * 0.5f / distance) * Mathf.Rad2Deg;
}
}
void Update() {
check();
}
}