Nerd For Tech
Published in

Nerd For Tech

Working with Animation Events in Unity

Now that we have a ledge grab in place, let’s get to work on creating the animation to climb up our ledge and stay in that position upon reaching the top. To start, we will find a new animation to work with and add it to our assets along with the animator grid. We will have it currently just attached to the hanging idle animation, just so we can be sure that it works proper:

From here, we will need to create code within our player that will enable us to press a key to enable our ledge climb, but because we have turned off the controller upon attaching to the ledge, we need to be sure to add it in after the controller movement’s statement we have made:

From here, we can see how it looks within our scene by playing through it quick:

Now, if we wanted to have it switch to idle upon the end of the animation, we will have a slight issue with what ends up happening:

The reason this occurs is because we have our player’s position set to the end point when have attach to the ledge. As the animation does not override the current players position, we will now need to create a method in code that will enable us to have our character move to the new position upon ending the climbing animation. To do this, we first need to figure out the rough position our player will be standing in to have it become the position we want our player to snap to upon finishing the climb up animation. Once we have it assigned and the coordinates placed, we have to make a function within our ledge detector script that we can call from to place our player in that position:

Within our player script, we will want to create a new method in which our player’s position becomes the spot that we figured our earlier, as well as turn off our ledgegrab bool along with reactivating our player controller so that we can continue playing the game. From here, we will want to go create a new script, but this time it will be for our animation, so it will be a little different of a method:

What this does is create a behavior script for our animation to work from, giving us the shell of options we can choose from, and we pick whichever we feel works best for the situation we are in:

In this case, we want the OnStateExit condition, and we will call upon the player object to call the newly made method. This will play once our climb up animation has ended. With all of this now finished up, let’s see if it works:

It almost works perfectly, aside from the snap glitch that appears at the end of the animation. This is because we have a transition duration set on the transition, which we do not want:

Once we take this off, we can see how our final results look:

There we have it, we have manage to create a climb up animation within our game that allows us to snap to the new position and enables everything within the game so that we can keep playing.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store