
using UnityEngine; public class PlayerController : MonoBehaviour { public float turnspeed = 10; float ver = 0; float hor = 0; void Update() { hor = Input.GetAxis("Horizontal"); ver = Input.GetAxis("Vertical"); } void FixedUpdate() { if (hor != 0 || ver != 0) { //轉身 Rotate(hor, ver); } } void Rotate(float hor, float ver) { //獲取方向 Vector3 dir = new Vector3(hor, 0, ver); //將方向轉換為四元數 Quaternion quaDir = Quaternion.LookRotation(dir, Vector3.up); //緩慢轉動到目標點 transform.rotation = Quaternion.Lerp(transform.rotation, quaDir, Time.fixedDeltaTime * turnspeed); } }
轉載:https://blog.csdn.net/qq_25210959/article/details/51713408