相机跟随角色移动镜头缩放方法

admin3年前unity基础API723

这种方法最简单可以实现相机跟随角色平移


1:计算相机和目标物体的距离 

 offset = this.transform.position - target.transform.position;


2:Update方法中实时变化相机位置


 this.transform.position = target.transform.position + offset;



3:镜头缩放

  //镜头缩放

    public void CamRotation() {


        if (Input.GetAxis("Mouse ScrollWheel") != 0)
        {
            //鼠标滚动滑轮 值就会变化
            if (Input.GetAxis("Mouse ScrollWheel") < 0)
            {
                //范围值限定
                if (Camera.main.fieldOfView <= 100)//摄像机采用Perspective透视
                    Camera.main.fieldOfView += 2;
                if (Camera.main.orthographicSize <= 20)//摄像机采用Orthograpic正交
                    Camera.main.orthographicSize += 0.5F;
            }
            //Zoom in  
            if (Input.GetAxis("Mouse ScrollWheel") > 0)
            {
                //范围值限定
                if (Camera.main.fieldOfView > 2)//摄像机采用Perspective透视
                    Camera.main.fieldOfView -= 2;
                if (Camera.main.orthographicSize >= 1)//摄像机采用Orthograpic正交
                    Camera.main.orthographicSize -= 0.5F;
            }
        }

    }


相关文章

Unity场景跳转方法

实现跳转不到的场景,需要先把场景添加到Build中首先引入命名空间using UnityEngine.SceneManagement;然后使用方法跳转 SceneManager.L...

Unity移动操作物体

if (Input.touchCount == 1)         {  ...

通过刚体移动的方法

1:MovePosion移动的新的位置,传入的参数是最新的位置 一般是旧的位置加上变化的参数2:velocity这个方法是瞬间给物体一个恒定的速度,将物体提升至该速度 3:addF...

unity 新的输入系统input system 使用

unity 新的输入系统input system 使用

1:首先在包管理器里安装 搜索"input system"2: 创建input acionts3:生成代码  勾选后点击右小角应用就可以自动生成相关代...

Unity寻找子物体的几种方法

    1: transform.Find()寻找子物体   transform.Find(“子物体名字”)2: GameObject.FindGam...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。