混合动画
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 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库