using System.Collections;using System.Collections.Generic;using UnityEngine;publicclassPlayerMovement:MonoBehaviour{publicCharacterController controller;publicfloat speed =12f;// Update is called once per framevoidUpdate(){float x = Input.GetAxis("Horizontal");float z = Input.GetAxis("Vertical");Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);}}
放在人物底下
using System.Collections;using System.Collections.Generic;using UnityEngine;publicclassMouseLook:MonoBehaviour{publicfloat mouseSensitivity =100f;publicTransform playerBody;float xRotation =0f;// Start is called before the first frame updatevoidStart(){
Cursor.lockState = CursorLockMode.Locked;}// Update is called once per framevoidUpdate(){float mouseX = Input.GetAxis("Mouse X")* mouseSensitivity * Time.deltaTime;float mouseY = Input.GetAxis("Mouse Y")* mouseSensitivity * Time.deltaTime *2f;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation,-90f,90f);
transform.localRotation = Quaternion.Euler(xRotation,0f,0f);
playerBody.Rotate(Vector3.up * mouseX);}}