0
点赞
收藏
分享

微信扫一扫

Unity相机缩放到(Zoom in)某个物体

乱世小白 2022-03-20 阅读 49

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour {
    public GameObject[] objs;
    public Camera mCamera;
    int index = 0;
	
	// Update is called once per frame
	void Update () {
        if(Input.GetKeyDown(KeyCode.Space))
        {
            GameObject obj = objs[index % objs.Length];
            MeshRenderer mr = obj.GetComponent<MeshRenderer>();

            //物体最大长度
            float a = mr.bounds.size.magnitude;

            //物体相机的距离
            float b = Vector3.Distance(mCamera.transform.position, obj.transform.position);

            //视场角,单位为度
            float angle = Mathf.Rad2Deg * Mathf.Atan(0.5f * a / b);            
            mCamera.fieldOfView = 2.0f * angle;

            //正对着物体
            mCamera.transform.LookAt(mr.bounds.center);

            index++;
        }		
	}
}

 本来物体是这样放的

按空格键可以依次缩放到某个物体 

 

 

举报

相关推荐

0 条评论