久违第一次,镜头方向移动与镜头的旋转

using UnityEngine;
using System.Collections;

public class PlayerMove : MonoBehaviour {

    public float moveSpeed = 2f; 
    public float rotateSpeed = 5f;

void Update () {
        
        //控制物体移动按照镜头方向
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        if(h!=0||v!=0)
        {
            Vector3 MoveDirection = new Vector3(h, 0, v);
            float y = Camera.main.transform.rotation.eulerAngles.y;
            MoveDirection = Quaternion.Euler(0, y, 0) * MoveDirection;
            transform.Translate(MoveDirection * Time.deltaTime * moveSpeed, Space.World);
        }


        //镜头视角的旋转Q(左)E(又)
        if(Input.GetKey(KeyCode.Q))
        {
            transform.Rotate(-Vector3.up * Time.deltaTime * rotateSpeed);
        }
        if (Input.GetKey(KeyCode.E))
        {
            transform.Rotate(Vector3.up * Time.deltaTime * rotateSpeed);
        }
}
}

猜你喜欢

转载自blog.csdn.net/weixin_41833703/article/details/80773508