• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
~Joke_crazy
爱生活,爱拉芳!
   首页    新随笔       管理     

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

posted @ 2018-11-23 14:19  ~Joke_crazy  阅读(1740)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3