#Transition from walk to idle and vice versa fires an extra time

1 messages · Page 1 of 1 (latest)

warped zealot
#

Hey all, I an dipping my toe in the new input system. After a tutorial, I have things working, but I am running into an issue where my idle and walk animations are not behaving as I should. When I trigger from walk to idle and vice versa, the other fires at the end and I am not sure why. Below is my movement code:

public class Succubus : MonoBehaviour
 {
    BoxCollider2D collidr;
    Rigidbody2D rb;
    Animator animator;
    Vector2 _moveDirection;
    ...
    public InputActionReference move;
    public PlayerData data;
    void Start()
    {
        ...
        collidr = GetComponent<BoxCollider2D>();
        rb = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
        data.HP = data.MaxHP;
    }
    void Update()
    {
        _moveDirection = move.action.ReadValue<Vector2>();       
    }
    void FixedUpdate()
    {
        if(_moveDirection.x != 0)
        {
            spriteRenderer.flipX = _moveDirection.x < 0;
            animator.SetTrigger(Trigger.walk_);
            rb.linearVelocityX = _moveDirection.x * data.velocity; // data.velocity = 7
        }
        else
        {
            rb.linearVelocityX = 0;
            animator.SetTrigger(Trigger.idle_);
        }
    }
    void OnEnable()
    {
        ...
        move.action.started += Move;
    }
    void OnDisable()
    {
        ...
        move.action.started -= Move;
    }
    void Move(InputAction.CallbackContext obj)
    {
        animator.SetTrigger(Trigger.walk_);
    }
    ...
}
#

There are no errors in the console.

undone dew
#

youre doing it each FixedUpdate

warped zealot
#

aaah,

#

yeah

undone dew
#

You only want to set the trigger when you switch

warped zealot
#

Thank you, that's a good point

#

okay, so in that case, where should I invoke the trigger?

#

I tried putting in my Move()

    void Move(InputAction.CallbackContext obj)
    {
        animator.SetTrigger(Trigger.walk_);
    }

and it doesn't fire at all

undone dew
#

you need it to happen when you transition from walking to idle and vice versa. But really I think a Trigger is the wrong choice here

#

you should make it a bool

#

it can be done with triggers but the code is more complex

warped zealot
#

gotcha. So a kind of state where it will keep its "true" while the walking is engaged.

undone dew
#

if you use triggers, you need to compare what's happening this frame (walking? idle?) with last frame and set the trigger when it changes

undone dew
#

and set the transitions up appropriately in the state machine

#

(you have to change the param in the state machine to a bool too)

warped zealot
#

Already on it