ZERO_BEYOND

博客园 首页 联系 订阅 管理

混合动画

 1     private Vector2 lookDir = new Vector2(0, -1);
 2     private Vector2 move = new Vector2(0, 0);
 3     void Update()
 4     {
 5         //移动
 6         float horizontal = Input.GetAxis("Horizontal");
 7         float vertical = Input.GetAxis("Vertical");
 8         //方向
 9         move = new Vector2(horizontal, vertical);
10         //判断是否近似相等
11         if (!Mathf.Approximately(move.x, 0) || !Mathf.Approximately(move.y, 0))
12         {
13             lookDir = move;
14             //归一化
15             lookDir.Normalize();
16         }
17         //将值传入控制人物动画切换
18         GetComponent<Animator>().SetFloat("Look X", lookDir.x);
19         GetComponent<Animator>().SetFloat("Look Y", lookDir.y);
20         //求模,判断是否在移动
21         GetComponent<Animator>().SetFloat("Speed", move.magnitude);
22         Vector2 _pos = transform.position;
23         _pos += speed * move * Time.deltaTime;
24         //限制移动范围
25         _pos = new Vector2(Mathf.Clamp(_pos.x,-27,32), Mathf.Clamp(_pos.y, -10, 20));
26         //刚体控制移动
27         GetComponent<Rigidbody2D>().MovePosition(_pos);
28     }
posted on 2023-05-05 05:06  ZERO_BEYOND  阅读(113)  评论(0编辑  收藏  举报