Unity 漫游镜头代码实现

镜头移动
public class 镜头移动 : MonoBehaviour {
    private Transform cameraTransform;
    [SerializeField]private Transform characterTransform;
    private Vector3 cameraRotation;
    public float MouseSensitivity;
    public Vector2 MaxminAngle;
    private void Start()
    {
//获取组件
        cameraTransform = transform;
    }
    void Update ()
    {
        MouseSensitivity = 5;
//鼠标控制
        var tmp_MouseX = Input.GetAxis("Mouse X");      
        var tmp_MouseY = Input.GetAxis("Mouse Y");
//鼠标灵敏度
        cameraRotation.x -= tmp_MouseY * MouseSensitivity;
        cameraRotation.y += tmp_MouseX * MouseSensitivity;
//限制上下看的范围
        cameraRotation.x = Mathf.Clamp(cameraRotation.x,-65,65);
//在世界空间中变换的旋转
        cameraTransform.rotation = Quaternion.Euler(cameraRotation.x, cameraRotation.y, 0);
        characterTransform.rotation = Quaternion.Euler(0, cameraRotation.y, 0);
  }
}
人物移动
public class Move : MonoBehaviour
{
    public CharacterController controller;
    public float speed = 10.0f;
    public float gravity = -9.8f;

// Use this for initialization
void Start()
    {
    }
 void Update()
    {
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");
        //Vector3 move = new Vector3(x, 0f, z);// × global movement, we dont want
        Vector3 move = transform.right * x + transform.forward * z;// move along the local coordinates right and forward​
        controller.Move(move * speed * Time.deltaTime);
    }
posted @ 2023-06-10 20:18  支阿怪🔥  阅读(104)  评论(0编辑  收藏  举报