我有一些代码可以让玩家上下左右移动。
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")中的get键切换为Input.GetKeyDown()和Input.GetKeyUp(),但都不起作用。
我也尝试过使用混合树,但我想不出该怎么做。
我是Unity的新手,所以一个简单的答案会很好。
发布于 2020-05-16 07:31:40
打开动画窗口
创建“状态”
将动画剪辑设置为'motion‘
在左上角单击'parameters‘选项卡
单击parameters旁边的加号
点击‘触发器’
为触发器命名
右键单击“Any State”并进行转换,以将一条线拖到您创建的新“State”上
点击'Any State‘和你创建的'State’之间的线
在'conditions‘下,按加号并添加您刚刚创建的触发器
在代码编写中
animator.SetTrigger("triggername");当你想要转换到之前提到的动画状态重复你的所有动画,链接他们从‘任何状态’
https://stackoverflow.com/questions/61829871
复制相似问题