您当前的位置: 首页 >  unity

程序员正茂

暂无认证

  • 1浏览

    0关注

    283博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

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

程序员正茂 发布时间:2022-03-20 12:46:00 ,浏览量:1

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

            //物体最大长度
            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++;
        }		
	}
}

 本来物体是这样放的

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

 

 

关注
打赏
1660743125
查看更多评论
立即登录/注册

微信扫码登录

0.0408s