Why doesn't my character attack while moving diagonally?

Viewed 69

enter image description hereI've followed some simple tutorials on Udemy and Youtube, and I created a player controller that involves walking and attacking. I cannot seem to figure out why I can't attack while holding down 2 directional inputs. The attack animation plays when I am moving up and right, but not down/right, down/left, or up/left.

Here is the code I am using

    void Update()
{
    //Movement

    theRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), (Input.GetAxisRaw("Vertical"))) * moveSpeed;

    myAnim.SetFloat("moveX", theRB.velocity.x);
    myAnim.SetFloat("moveY", theRB.velocity.y);

    //Attack

    if (Input.GetKeyDown(KeyCode.Space))
    {
        Attack();
    }

    void Attack()
    {
        myAnim.SetTrigger("Attack");
    }


}

I understand that the Attack() method has no effect yet besides the animation, but I cannot get the animation to play while moving diagonally. Any help would be appreciated.

1 Answers

Ok, let me explain you bitwise that you can't run 2 animations at a time when using Unity animator. I think that you might be using kinda this

No use of any state

Instead of beating from any state. I mean see it will be on IDLE by default and it will only attack if the character is on IDLE animation. So to prevent this you can use

With use of any state

What it will do is it will set the player even if it's walking. You must set back transition to IDLE if not walking and back transition to walking if it's walking. If you are doing this mistake I hope so you got your answer.

Related