0
\$\begingroup\$

I have a plane that can get its scale changed during the game, and I want to make the camera view it completely every time the plane has its scale changed. The only camera is set to perspective projection.

I found this page which tells me how to resize the plane (I do not understand the unity of measure used there) to be viewed by the camera, and I want the reverse.

I found something similar here.

I also found other not-very-relevant answers by googling "unity camera fits a plane", "unity move camera to show an object" (one of the results is this), "unity field of view to fit an object" (one of the results is this).

I think I found the answer here, but it is hard for me to do the maths right now. I also found an answer here but I wish that someone explained me the mathematics (also, I do not know how to get the bounds).

My relevant previous question with the same situation is here. There is a link to download a sample project, and a few screenshots.

Thank you.

\$\endgroup\$
2
  • \$\begingroup\$ Is this gamedev.stackexchange.com/questions/175034/… what you are trying to achieve? Is it different from that question (for example, only on y, height camera should be matched)? I understand that math can be a bit different in your version because you might not need it to be movable, rotatable... It would still probably match main logic though. I am trying to find reasons why this is not a duplicate question. \$\endgroup\$ Jan 15 '20 at 10:38
  • \$\begingroup\$ I think that that is what I am trying to achieve. It is different in that I have the solution but it is not mathematically explained. I would post it on math.stackexchange.com but I feel too beginner in maths to ask there and I do not know how to express myself and I did not try anything that can be easily expressed mathematically. Thank you. \$\endgroup\$ Jan 15 '20 at 10:51
1
\$\begingroup\$

I have found the answer here. Although it is not completely explained mathematically in my opinion, it works well.

The code I use is like this:

private void Start()
{
    // ...

    PositionCamera();
}

/// <summary>
/// Source: https://forum.unity.com/threads/dynamic-loaded-object-fit-to-screen-size.349794/
/// </summary>
void PositionCamera()
{
    GameObject ObjectToView = GameObject.Find("Background");
    Camera camera = Camera.main;

    Bounds objectBounds = ObjectToView.GetComponent<Renderer>().bounds;
    Vector3 objectFrontCenter = objectBounds.center - ObjectToView.transform.forward * objectBounds.extents.z;

    //Get the far side of the triangle by going up from the center, at a 90 degree angle of the camera's forward vector.
    Vector3 triangleFarSideUpAxis = Quaternion.AngleAxis(90, ObjectToView.transform.right) * transform.forward;
    //Calculate the up point of the triangle.
    const float MARGIN_MULTIPLIER = 1f;
    Vector3 triangleUpPoint = objectFrontCenter + triangleFarSideUpAxis * objectBounds.extents.y * MARGIN_MULTIPLIER;

    //The angle between the camera and the top point of the triangle is half the field of view.
    //The tangent of this angle equals the length of the opposing triangle side over the desired distance between the camera and the object's front.
    float desiredDistance = Vector3.Distance(triangleUpPoint, objectFrontCenter) / Mathf.Tan(Mathf.Deg2Rad * camera.GetComponent<Camera>().fieldOfView / 2);

    camera.transform.position = -camera.transform.forward * desiredDistance + objectFrontCenter;
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .