我有一些代码可以让一个玩家向上、向下、向左、向右移动。
public class playerMovement : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D rb;
public Animator animator;
public SpriteRenderer sr;
Vector2 movement;
bool walking = false;
private void Update()
{
//Inputs
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
if (Input.GetKey("left") || Input.GetKey("right") || Input.GetKey("up") || Input.GetKey("down")
{
walking = true;
}
else
{
walking = false;
}
Animate();
}
private void Animate()
{
if (Input.GetKey("down") && walking == false)
{
sr.flipX = false;
animator.Play("idle_front");
}
else if (Input.GetKey("down") && walking)
{
sr.flipX = false;
animator.Play("walk_front");
}
else if (Input.GetKey("up") && walking == false)
{
sr.flipX = false;
animator.Play("idle_back");
}
else if (Input.GetKey("down") && walking)
{
sr.flipX = false;
animator.Play("walk_back");
}
else if (Input.GetKey("left") && walking == false)
{
sr.flipX = false;
animator.Play("idle_side");
}
else if (Input.GetKey("down") && walking)
{
sr.flipX = false;
animator.Play("walk_side");
}
else if (Input.GetKey("right") && walking == false)
{
sr.flipX = true;
animator.Play("idle_side");
}
else if (Input.GetKey("right") && walking)
{
sr.flipX = true;
animator.Play("walk_side");
}
}
public void FixedUpdate()
{
//Movement
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
}
移动工作完全正常,但角色只是停留在空闲状态。我试过将 Input.GetKey("left") || Input.GetKey("right") || Input.GetKey("up") || Input.GetKey("down")
到 Input.GetKeyDown()
和 Input.GetKeyUp()
但这些都没有用。
我也试过使用Blend Trees,但我不知道怎么做。
我对Unity相当陌生,所以一个简单的答案将是巨大的。
打开动画师窗口
创设'国家'
将动画片段设置为 "运动"。
在左上角点击 "参数 "标签
点击参数旁边的加号
点击 "触发"。
命名你的触发器
在 "任何状态 "上点击右键,然后拖动一条线到你创建的新的 "状态 "上进行过渡。
点击 "任何国家 "和您创建的 "国家 "之间的那条线
在'条件'下按加号,并添加你刚刚创建的触发器。
编写
animator.SetTrigger("triggername");
当你想将所有动画过渡到前面提到的动画状态重复时,将它们从 "任意状态 "中连接起来