登录后复制
// Update is called once per frame void Update () { float ver = Input.GetAxis("Vertical"); transform.position += transform.forward * ver * Time.deltaTime * 5; float hor = Input.GetAxis("Horizontal"); transform.Rotate(transform.up * hor * Time.deltaTime * 100); }----------------------------------------------------------------------------- private void Update() { if (Input.GetKey(KeyCode.W) | Input.GetKey(KeyCode.UpArrow)) //前 { this.transform.Translate(Vector3.forward * m_speed * Time.deltaTime); } if (Input.GetKey(KeyCode.S) | Input.GetKey(KeyCode.DownArrow)) //后 { this.transform.Translate(Vector3.forward * -m_speed * Time.deltaTime); } if (Input.GetKey(KeyCode.A) | Input.GetKey(KeyCode.LeftArrow)) //左 { this.transform.Translate(Vector3.right * -m_speed * Time.deltaTime); } if (Input.GetKey(KeyCode.D) | Input.GetKey(KeyCode.RightArrow)) //右 { this.transform.Translate(Vector3.right * m_speed * Time.deltaTime); } }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.
参考之前的记录 Unity的自动寻路NavMessAgent系统(一)_幻世界
左右键旋转,前后键前进后退,相机设置为目标子节点固定相对位置效果如上图
鼠标控制相机旋转,效果如上图
登录后复制
// 控制玩家移动 void Update () { float ver = Input.GetAxis("Vertical"); transform.position += transform.forward * ver * Time.deltaTime * 5; }1.2.3.4.5.
登录后复制
控制相机视角 //鼠标左键控制上下转动 if (Input.GetMouseButton(0)) { transform.Rotate(-Input.GetAxis("Mouse Y") * 2, 0, 0); } //鼠标右键控制左右转动 if (Input.GetMouseButton(1)) { transform.Rotate(0, Input.GetAxis("Mouse X") * 2, 0); }1.2.3.4.5.6.7.8.9.10.11.
两个脚本,PlayerMovement控制身体的位移,PlayerCamera控制自己的相机的旋转
登录后复制
public class PlayerMovement : MonoBehaviour{ public CharacterController controller; public float speed = 12f; public float gravity = 10f; public float jumpHeight = 2f; public Transform groundCheck; public float groundDistance = 0.4f; public LayerMask groundMask;//地面layer Vector3 velocity; bool isGrounded;//是否落地 float x; float z; // Update is called once per frame void Update() { x = Input.GetAxis("Horizontal"); z = Input.GetAxis("Vertical"); Vector3 move = transform.right * x + transform.forward * z; controller.Move(move * speed * Time.deltaTime); isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask); if (isGrounded && velocity.y < 0) { velocity.y = -2f; } if (Input.GetButtonDown("Jump") && isGrounded) { velocity.y = Mathf.Sqrt(jumpHeight * gravity); } velocity.y -= gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime); }}1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.
登录后复制
public class PlayerCamera : MonoBehaviour{ private float mouseSensitivity = 300; private Transform playerBody; // Start is called before the first frame update void Awake() { Cursor.lockState = CursorLockMode.Locked; playerBody = transform.parent; } // Update is called once per frame void Update() { float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime; playerBody.Rotate(Vector3.up * mouseX); float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime; transform.localEulerAngles -= new Vector3(mouseY, 0f, 0f); }}1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删