ZERO_BEYOND

博客园 首页 联系 订阅 管理
  56 随笔 :: 0 文章 :: 1 评论 :: 15120 阅读

混合动画

复制代码
 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   ZERO_BEYOND  阅读(129)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示