1

How can I set up the appropriate size of the Frustum such that a moving camera needs to keep an object (such as the 3D model) completely in a scene at all times that it must not get so close that part of that object is cut off.

The model doesn't move.

1
  • You could use your Camera > Clipping Planes > Near value (e.g. the default 0.3f) plus a minor security padding (e.g. 0.01f) as radius for an isTrigger SphereCollider, which OnTriggerEnter stops moving towards the given target object. Note it might be useful if you posted your current relevant camera movement code. Mar 24, 2020 at 15:49

1 Answer 1

0

If camera is always pointed to your object and the object is in the center of screen you can solve this problem easily here https://docs.unity3d.com/Manual/FrustumSizeAtDistance.html .But in general, this class would do the task. First, you need to consider a virtual sphere around your object which includes your object entirely and this code avoids the object get out of the screen.

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();
    }
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.