Unity中实现人物平滑转身


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);
    }
}
View Code

转载:https://blog.csdn.net/qq_25210959/article/details/51713408


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM