Unity3D 角色移动脚本的实现 2019/11/10

注意本脚本是用刚体移动一定要给物体叫加上刚体组件,并且人物动作动画使用mecanima的混合树“forward”播放

[Header("===== 控制移动和方向的各项数值 ======")]
    private float h = 0.0f;
    private float v = 0.0f;
    //向量
    private Vector3 moveDir;
    //移动变量
    public float moveSpeed = 2.0f;
    //旋转速度
    public float rotSpeed = 100.0f;
    //奔跑速度
    public float runSpeed = 4.0f;
    private float mag;
    private float velocityh;
    private float velocityv;
    private float targeth;
    private float targetv;
    private Transform tr;
    private Animator ani;
    private Rigidbody rb;
    public bool run;


    //输入特殊按键
    public string keyA = "left shift";
    // public string keyB;
    // Start is called before the first frame update
    void Awake()
    {
        tr = GetComponent<Transform>();
        ani = GetComponent<Animator>();
        rb = GetComponent<Rigidbody>();


    }

    // Update is called once per frame
    void Update()
    {

        targeth = Input.GetAxis("Horizontal");
        targetv = Input.GetAxis("Vertical");
        run = Input.GetKey(keyA);


        h = Mathf.SmoothDamp(h, targeth, ref velocityh, 0.1f);
        v = Mathf.SmoothDamp(v, targetv, ref velocityv, 0.1f);

        Debug.Log("H=" + h.ToString());
        Debug.Log("V=" + v.ToString());

        


        //计算前后左右移动的方向向量


        moveDir = (Vector3.forward * v) + (Vector3.right * h);
        mag = Mathf.Sqrt(targeth * targeth + targetv * targetv);
        if (mag > 1.0f)
        {
            mag = 1.0f;
        }


        //移动动画

         float runani = ((run) ? 2.0f : 1.0f);

         ani.SetFloat("forward", mag *Mathf .Lerp (ani .GetFloat("forward") , runani,0.5f));
         //走动
         if (mag > 0.1f)
         {
             Vector3 targetforward = Vector3.Slerp(tr.forward, moveDir, 0.3f);
             tr.forward = targetforward;
         }
       //下面是用bool来实现人物动画播放
        /* if (moveDir != Vector3.zero) 
         {
             tr.rotation = Quaternion.LookRotation( moveDir  );

             if (run) {
                 ani.SetBool("run", true);

             }

             ani.SetBool("walk", true);

         }  else
         {
             ani.SetBool("walk", false);
         } */


    }





    void FixedUpdate()
    {
        //用刚体来使物体移动(移动速度*方向向量*tiem.fixeddeltatime)

       rb.position += moveSpeed * moveDir * Time.fixedDeltaTime * ((run) ? 2.0f : 1.0f);
    
        
    }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 更新:【面试题含答案】http://bbs.9ria.com/thread-288394-1-1.html 高频问...
    好怕怕阅读 10,409评论 3 52
  • 前言:这篇文章是之前自己博客里的,这次重新用Markdown排下版,给需要的人, 想要成为一个优秀的U3D程序员...
    道阻且长_行则将至阅读 5,726评论 0 20
  • 一.刚体组件的属性介绍 刚体能让你的游戏对象被物理引擎所控制,它能通过受到推力和扭力来实现真实的物理表现效果。所有...
    沉麟阅读 5,177评论 0 0
  • 最先执行的方法是: 1、(激活时的初始化代码)Awake,2、Start、3、Update【FixUpdate、L...
    困卡阅读 9,844评论 0 8
  • 1.什么是渲染管道? 是指在显示器上为了显示出图像而经过的一系列必要操作。 渲染管道中的很多步骤,都要将几何物体从...
    爱机车的异乡人阅读 3,795评论 0 1

友情链接更多精彩内容