我用C#做了一个unity程序,其中玩家可以移动和跳跃。
当我运行这段代码时,玩家的 idle animation
和玩家的 jumping animation
在动画师中同时播放这两个动画,导致了一个故障,而且看起来不是很好。
这是我使用的代码。
if (Input.GetKey("d") || Input.GetKey("right"))
{
rb2D.velocity = new Vector2(Forwardspeed, 0f);
animator.Play("Player_run");
spr.flipX = false;
}
else if (Input.GetKey("a") || Input.GetKey("left"))
{
rb2D.velocity = new Vector2(-Forwardspeed, 0f);
animator.Play("Player_run");
spr.flipX = true;
}
else if (!Input.anyKeyDown)
{
rb2D.velocity = new Vector2(0, 0);
animator.Play("Player_idle");
}
else if (Input.GetKey("space"))
{
rb2D.velocity = new Vector2(Forwardspeed, Uppwardforce);
animator.Play("Player_jump");
}
根据你的代码设计,为什么不把空闲作为一个 else条件呢?这样一来,如果你没有键下,你的播放器就会空闲下来,例如:。
if (Input.GetKey("d") || Input.GetKey("right"))
{
rb2D.velocity = new Vector2(Forwardspeed, 0f);
animator.Play("Player_run");
spr.flipX = false;
}
else if (Input.GetKey("a") || Input.GetKey("left"))
{
rb2D.velocity = new Vector2(-Forwardspeed, 0f);
animator.Play("Player_run");
spr.flipX = true;
}
else if (Input.GetKeyDown("space"))
{
rb2D.velocity = new Vector2(Forwardspeed, Uppwardforce);
animator.Play("Player_jump");
}
else
{
rb2D.velocity = new Vector2(0, 0);
animator.Play("Player_idle");
}
希望能帮到你
为什么不这样做呢?
else if (Input.GetKey("a") || Input.GetKey("left"))
{
rb2D.velocity = new Vector2(-Forwardspeed, 0f);
animator.Play("Player_run");
spr.flipX = true;
if (Input.GetKey("space"))
{
rb2D.velocity = new Vector2(Forwardspeed, Uppwardforce);
animator.Play("Player_jump");
}
}
如果你不需要加力,你可以直接测试KeyDown。
if (Input.GetKeyDown("space"))
{
rb2D.velocity = new Vector2(Forwardspeed, Uppwardforce);
animator.Play("Player_jump");
}
你按住 "运行 "键,然后你只需脉冲键空间