- Home /
Question by
xAdamQ · Mar 16, 2017 at 08:29 PM ·
cameracamera-movementcamera-look
How to make camera focus on a game Object in play mode?
Hi, I want to make the camera focus on a game object, like that behavior we can do in the editor (Shift+F).
I need that because I am making a game has a lot of levels scenes and every scene has main level mesh, the orientation is Vertical and Horizontal so that mesh sometimes doesn't appear fully.
Comment
Answer by Alteracia · Oct 31, 2017 at 04:55 PM
Hi, I was searching solution for similar problem. But i needed to focus camera from static point (edge of object). Also I have box (target) in screen which have scale. I hope it helps someone.
public Transform target;
void CameraFocus () {
Vector3 pointOnside = target.position + new Vector3 (target.localScale.x * 0.5f, 0.0f, target.localScale.z * 0.5f);
float aspect = (float)Screen.width / (float)Screen.height;
float maxDistance = (target.localScale.y * 0.5f) / Mathf.Tan (Mathf.Deg2Rad * (Camera.main.fieldOfView / aspect));
Camera.main.transform.position = Vector3.MoveTowards (pointOnside, target.position, -maxDistance);
Camera.main.transform.LookAt (target.position);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612113234im_/https://answers.unity.com/themes/thub/images/avi.jpg)