写一个uity人物移动就播放动画的代码
时间: 2023-04-07 09:03:45 浏览: 76
好的,以下是一个简单的Unity人物移动并播放动画的代码:
```csharp
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed = 5f;
private Animator animator;
private void Start()
{
animator = GetComponent<Animator>();
}
private void Update()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(horizontal, 0f, vertical);
transform.position += movement * speed * Time.deltaTime;
if (movement != Vector3.zero)
{
animator.SetBool("isWalking", true);
transform.rotation = Quaternion.LookRotation(movement);
}
else
{
animator.SetBool("isWalking", false);
}
}
}
```
这个代码会让你的人物在水平和垂直方向上移动,同时播放行走动画。当人物移动时,它会面向移动的方向。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)