控制摄像机(camera)的移动,以及摄像机的视角随着鼠标箭头的改变而改变

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

public class CameraControl : MonoBehaviour {

    public float cameraSensitivity = 90;
    public float climbSpeed = 4;

    public float normalMoveSpeed = 10;
    public float slowMoveSpeed = 0.25f;
    public float fastMoveSpeed = 3;

    private float rotationX = 0.0f;
    private float rotationY = 0.0f;

    void Start()
    {
        rotationX = transform.eulerAngles.y;
    }

    void Update () {
        #region 控制摄像机的方向,鼠标箭头在哪,摄像机的方向就指向那

        var dataTime = Time.deltaTime;
        rotationX += Input.GetAxis("Mouse X") * cameraSensitivity * dataTime;
        rotationY += Input.GetAxis("Mouse Y") * cameraSensitivity * dataTime;
        rotationY = Mathf.Clamp(rotationY, -90, 90);

        var tempRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
        tempRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);
        transform.localRotation = Quaternion.Slerp(transform.localRotation, tempRotation, dataTime * 6.0f);

        #endregion

        #region 利用方向键控制摄像机的移动

        if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
        {
            transform.position += transform.forward * (normalMoveSpeed * fastMoveSpeed) * Input.GetAxis("Vertical") * dataTime;
            transform.position += transform.right * (normalMoveSpeed * fastMoveSpeed) * Input.GetAxis("Horizontal") * dataTime;
        }
        else if(Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
        {
            transform.position += transform.forward * (normalMoveSpeed * slowMoveSpeed) * Input.GetAxis("Vertical") * dataTime;
            transform.position += transform.right * (normalMoveSpeed * slowMoveSpeed) * Input.GetAxis("Horizontal") * dataTime;
        }
        else
        {
            transform.position += transform.forward * normalMoveSpeed * Input.GetAxis("Vertical") * dataTime;
            transform.position += transform.right * normalMoveSpeed * Input.GetAxis("Horizontal") * dataTime;
        }

        #endregion

        #region 利用Q和E来控制摄像机的上下移动

        if (Input.GetKey(KeyCode.Q))
        {
            transform.position -= transform.up * climbSpeed * dataTime;
        }

        if (Input.GetKey(KeyCode.E))
        {
            transform.position += transform.up * climbSpeed * dataTime;
        }

        #endregion
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值