using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { [SerializeField] private float moveSpeed = 7f; private bool isWalking; private void Update() { Vector2 inputVector = new Vector2(0, 0); // getkey会一直返回true,而getkeydown只会在按下的一帧返回true if (Input.GetKey(KeyCode.W)) { inputVector.y += 1; } if (Input.GetKey(KeyCode.S)) { inputVector.y -= 1; } if (Input.GetKey(KeyCode.A)) { inputVector.x -= 1; } if (Input.GetKey(KeyCode.D)) { inputVector.x += 1; } inputVector = inputVector.normalized; Vector3 moveDir = new Vector3(inputVector.x, 0f, inputVector.y); transform.position += moveDir * Time.deltaTime * moveSpeed; isWalking = (inputVector != Vector2.zero); float rotateSpeed = 10f; transform.forward = Vector3.Slerp(transform.forward, moveDir, Time.deltaTime * rotateSpeed); } public bool IsWalking() { return isWalking; } }