banner



How To Know When An Animation Stop In Unity Animation Controller

Unity Guide

A quick guide about how to work with animation states in Unity

Objective: Use animation states to handle the player's position afterward executing an animation.

In the last mail service I covered how to create a ledge catch mechanic for our histrion with Unity. Now, information technology's time to work with the animation states in order to handle the position of our player subsequently an blitheness ends in our platformer game.

Current animation states

If you oasis't seen my last posts, I recommend you to check them in example that you don't know how to add new animations with Unity:

In those posts we've implemented the adjacent animation states for our player:

New animation

So, in guild to follow the last mail service work (where we implemented a ledge grab blitheness), allow'south implement a new animation to display our histrion climbing up the ledge:

This animation was obtained from Mixamo for free.

Then, to add and handle the blitheness in a new country let'south:

  • Drag the file into the Animator window.
  • Create a new transition from the ledge grab animation.
  • Create a new parameter of blazon Trigger to trigger the animation.
  • Select the new transition coming from the ledge grab blitheness.
  • Disable the Has Exit Time property to avoid waiting for the final animation to end before executing the climbing animation.
  • Add the new Trigger parameter as the condition to transition to the climbing state.

Triggering the animation

Now, to indicate that the player is currently grabbing a ledge (from the last mail), let's open the Player script and use a new bool:

So, to trigger the blitheness when the player moves frontward, permit'due south do the next in the Update method:

  • Check if the histrion is currently grabbing a ledge.
  • Check if the actor is trying to move forward.
  • Trigger the animation by using the SetTrigger method in the animator component.

If we run the game in Unity, we'll see that the animation gets executed as expected, but, at that place's i detail after the animation ends. Nosotros tin notice that the player gameobject (collider) stills on the ledge and this is acquired because our animations are baked into the position and therefore they don't affect the root transform:

Working with the blitheness states

And so, what can we practise to set this state of affairs?……we tin employ the animation states to handle information technology!

To start, let'south create a new transition going from the climbing state to the idle country so that the player executes the idle animation subsequently climbing the ledge. Don't forget to leave the Has Get out Time property enabled equally the animation demand to stop before going into the idle state:

Then, in club to change the player'south position when the animation ends, let's add a StateMachineBehaviour script component to the climbing country:

Next, let'southward create a new empty gameobject to betoken the exact position of the player's model when the climbing animation ends:

And now, let's open the Player script and create a new variable (apart from the new bool) to salvage the Vector3 position of the gameobject that we but created:

Nosotros'll set the respective position when nosotros execute the public function that gets called every fourth dimension the role player grabs the ledge:

Now, let'due south create a new public role that will modify the player'south position when information technology climbs the current ledge and re-enable the character controller component to go along moving on the platforms:

Then, permit's open the StateMachineBehaviour script that we created for the climbing blitheness land:

In here, we can make use of the OnStateExit method to execute code when the player exits the climbing country:

Finally, as we take admission to the animator component of our player (from the parameter), we can get the Player script reference from its parent (the player) and call the respective public function to change the histrion's position:

If y'all want to know more near the StateMachineBehaviour component you can visit the Unity docs:

And now, if we run the game in Unity, nosotros'll see that the animation land works as expected later on climbing the ledge:

And that's it, we used the animation states to handle the role player's position with Unity! :D. I'll see you in the next postal service, where I'll exist showing new additions to the platformer game in Unity.

If you want to know more about me, experience free to connect with me on LinkedIn or visit my website :D

Source: https://medium.com/nerd-for-tech/working-with-animation-states-unity-ed82814c3fcd

Posted by: johnsonyoustion.blogspot.com

0 Response to "How To Know When An Animation Stop In Unity Animation Controller"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel