How to play animation on trigger enter and stop when animation reaches the end

Say I have two animator state - “move” and “still” and “still” is default state. When current gameobject is collided by a, say player object, it switch to “move” state. But how to switch back to still when the “move” animation plays to its end(since I don’t know when exactly it will end)?

Just make a transition link from “move” to “still”. The default transition is “Exit Time”, which switches when the “move” state is done.

So your animator controller might look something like this:

Parameters:

  • Move (trigger)

States:

  • [Any State]—(Move trigger)—>------>[still]
    [/LIST]
    The default state (orange) will be “still”.

You’ll need a little script or something to set the Move trigger:

void OnTriggerEnter(Collider other) {
    if (other.CompareTag("Player")) {
        GetComponent<Animator>().SetTrigger("Move");
    }
}
3 Likes

Thanks very much! I didn’t know that. It works well!

Glad to help!