Unity 鼠标控制第一人称摄像机视角

第一人称摄像机的实现:

鼠标的移动时的水平距离将决定摄像机在水平方向上的旋转角度,绕着旋转的轴应该为世界坐标系下的y轴

垂直方向上的距离将使摄像机上下旋转,这时候应该绕着自身坐标系下的x轴旋转

为什么不是绕着摄像机的x轴(transform.right),自己改了旋转的轴运行一下就知道了

public class FirstPersonalCamera : MonoBehaviour {
    public float speed = 5;
	// Update is called once per frame
	void Update () {
        //鼠标在这一帧移动的水平距离
        float x = Input.GetAxis("Mouse X");
        //绕世界坐标中的y轴旋转
        transform.Rotate(Vector3.up * x * speed, Space.World);
        //鼠标在这一帧移动的垂直距离
        float y = Input.GetAxis("Mouse Y");
        //绕自身的x轴转
        transform.Rotate(Vector3.right * -y * speed);
	}
}

 

 实现鼠标点击时,移动鼠标使得摄像机视角围绕主角旋转

public class CameraView : MonoBehaviour {
    Transform Player;
    private float x, y;
    public float rotateSpeed=5;
	// Use this for initialization
	void Awake() {
        Player = GameObject.FindGameObjectWithTag("Player").transform;
        //摄像机注视玩家
        transform.LookAt(Player);
	}
	// Update is called once per frame
	void Update () {
        if (Input.GetMouseButton(1))
        {
            //鼠标在这一帧移动的水平距离
            x = Input.GetAxis("Mouse X");
            //绕着玩家所在的点,世界的y轴旋转
            transform.RotateAround(Player.position, Vector3.up, x*rotateSpeed);
            //鼠标在这一帧移动的垂直距离
            y = Input.GetAxis("Mouse Y");
            //绕着玩家所在的点,摄像机的x轴旋转
            transform.RotateAround(Player.position, transform.right, y * rotateSpeed);
        }
	}
}

 

 

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值