写一个uity人物移动就播放动画的代码
时间: 2023-04-07 16:03:45 浏览: 242
好的,以下是一个简单的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);
}
}
}
```
这个代码会让你的人物在水平和垂直方向上移动,同时播放行走动画。当人物移动时,它会面向移动的方向。
阅读全文
相关推荐










