Hi, I am making a 2D, 3rd person hack-n-slash game (for easiest reference think Zelda: Link to the Past). Moving/walking around works just fine, as does slashing in the four directions (up/down/left/right). Each of the four “slash” animations has a Sprite Renderer component (to show the sword slashing), and a BoxCollider2D component (to activate and deal damage).
The issue is when I hold two different directions down at the same time, funky things start to happen.
For example, I’ll hold up, then down at the same time… the sprite stops its walk animation and stops moving (which is good!!), but then if I press the Slash Button, it will activate both animations (up/down) at the same time, however only the BoxCollider2D component from those animations will be activated, and not the Sprite Renderer component, which is very confusing.
To make things more confusing, if I press hold up then right/left, the full animations will play - meaning both the SpriteRenderer and BoxCollider2D component will both be activated (which is good!)… however, I still don’t want both the right/left animations playing at the same time in the first place (bad).
Basically, how do I get only one animation to play at once? Is there a way I can nullify/cancel out Input.GetAxisRaw if, say, I hold two different direction buttons at the same time, thus preventing two different animations from getting activated? I hope this makes sense.
Anyways here’s the code that controls moving, which sets up the direction the player is going to face, thus controlling the direction in which they will slash:
if (!isKnockingBack)
{
theRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")).normalized * moveSpeed;
playerAnim.SetFloat("moveX", theRB.velocity.x);
playerAnim.SetFloat("moveY", theRB.velocity.y);
if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1 ||
Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
{
playerAnim.SetFloat("lastMoveX", Input.GetAxisRaw("Horizontal"));
playerAnim.SetFloat("lastMoveY", Input.GetAxisRaw("Vertical"));
weaponAnim.SetFloat("dirX", Input.GetAxisRaw("Horizontal"));
weaponAnim.SetFloat("dirY", Input.GetAxisRaw("Vertical"));
}
}