美文网首页
Unity3D 角色移动脚本的实现 2019/11/10

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

作者: 墙上的钟声 | 来源:发表于2019-11-10 10:35 被阅读0次

    注意本脚本是用刚体移动一定要给物体叫加上刚体组件,并且人物动作动画使用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);
        
            
        }
    

    相关文章

      网友评论

          本文标题:Unity3D 角色移动脚本的实现 2019/11/10

          本文链接:https://www.haomeiwen.com/subject/wggobctx.html