How can I switch between idle and attacking animations while also keeping in mind direction.

I am pretty new to Unity and have just been messing around with animations. Currently, I have been using bools to check what direction the player is facing. I’m also referencing bools from the player animator in the sword script since the sword object is not attached to the player. With this I was able to have it switch between the two idle animation directions, however, the problem came up of it always playing the idle animations and never being able to switch to attacking. If anyone could help me with this problem and what I can do to fix it, as well any other advice on how I could clean things up, would be greatly appreciated.

public class SwordSwing : MonoBehaviour

public Animator swordAnimator;
public Animator playerAnimator;

public void Update()
{
    if (playerAnimator.GetFloat("lastMoveX") == 1)
    //(playerAnimator.GetBool("lastMoveXGreater") == true)
    {
        swordAnimator.SetBool("lastMoveXGreaterIdle", true);
        swordAnimator.SetBool("lastMoveXLessIdle", false);
    }

    if (playerAnimator.GetFloat("lastMoveX") == -1)
    // (playerAnimator.GetBool("lastMoveXLess") == true)
    {
        swordAnimator.SetBool("lastMoveXLessIdle", true);
        swordAnimator.SetBool("lastMoveXGreaterIdle", false);
    }

    if (Input.GetKeyDown(KeyCode.Mouse0))
    {
        swordAnimator.SetBool("pressedAttack", true);

        if (swordAnimator.GetBool("pressedAttack") == true && playerAnimator.GetBool("lastMoveXGreater") == true)
        {
            swordAnimator.SetBool("attackingXGreater", true);
        }

        if (swordAnimator.GetBool("pressedAttack") == true && playerAnimator.GetBool("lastMoveXLess") == true)
        {
            swordAnimator.SetBool("attackingXLess", true);
        }
    }
}

public void SwordSwingAnimationEvent()
{
    if (playerAnimator.GetFloat("lastMoveX") == 1)
    //(playerAnimator.GetBool("lastMoveXGreater") == true)
    {
        swordAnimator.SetBool("lastMoveXLessGreaterIdle", true);
    }

    if (playerAnimator.GetFloat("lastMoveX") == -1)
    // (playerAnimator.GetBool("lastMoveXLess") == true)
    {
        swordAnimator.SetBool("lastMoveXLessIdle", true);
    }

    swordAnimator.SetBool("pressedAttack", false);
}

}

This is where I am referencing the bools from in Player
void Update()
{
movement.x = Input.GetAxisRaw(“Horizontal”);
movement.y = Input.GetAxisRaw(“Vertical”);

    // animator.SetFloat("Horizontal", movement.x);
    playerAnimator.SetFloat("Speed", movement.sqrMagnitude);

    if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1)
    {
        playerAnimator.SetFloat("lastMoveX", Input.GetAxisRaw("Horizontal"));
    }

}

@Jezstuh
It depends on how your State Machine in Animator is set up. Note that you can play one animation in one layer of State Machine, so you can consider adding new layer for attacks and put idles on base layer. Then you can mark animation clips as “additive”, so you will be able to blend idle and attack animations :slight_smile: by the way, I don’t think idle animation should depend on direction player is looking currently at, you can simply mirror/rotate object


Check:

@Sonky108 Hello, thanks for your recommendation. I attempted adding a new layer for attacks and marked them as additive which seems to work with the parameters and the animations displaying that they are “playing” in the animator. However, the animations don’t actually play and are stuck on idle.

@Sonky108
I’m not an expert on animation, but I’m pretty sure you do not want to add an additional layer for an attack. At least, not if your attack is fully body (The whole body is animated, not just the upper body).


@Jezstuh
I think you might not realize that you need to actually turn the object, facing direction, (in code) or use root motion + rotation animation. Usually, we make all walking/attack/etc. facing 1 side and then we rotate the avatar in code so he is facing the correct side. (I’m not 100% sure because you did not provide your animations (name) nor your Animator)