Day 105: Working with Animation Events in Unity

Objective: Climb up the ledge and set the players new position.

Yesterday we worked on getting the player to grab onto a ledge and start the hanging idle animation. Now we have to work on starting the animation of climbing up from the ledge. We will start in the player script. In our player script update, we will check for the input on the key E. Once it is hit we will then check to see if we are holding the ledge. If we are set the animation trigger.

In the Animator, we will create a new trigger parameter.

Next is to set the condition of the transition from hanging idle to climb ledge. We will also check to make sure HoldingLedge is still true.

To finish off the animator section, we will have a transition line from ledge climb to Idle.

Now if we try and climb up the ledge our player will move back to the position of holding onto the ledge.

To fix this we will look into animation behaviours. In our animator we will select our climb ledge animation. In the inspector at the bottom we will Add Behaviour. For this one we will call ClimbUpBehaviour.

After it is created we will open it up. Once it opens you will noticed a lot of commented out code. These are used for StateMachineBehaviours. We will scroll down and un-comment OnStateExit method. This method will run as soon as the animation is finished.

In this method we will use the animator to grab the player script. Since the animation is on a child of the Player we will need to grab the parent to grab the script.

This will set the players position to be at the top of the platform we just climbed up. To set it we will need to move back to the player script and create a method. In this method we will set the transform.position to the activeledge.GetStandPos. We will cover this method in a second. Next is reenable the character controller. Finally, we will set the HoldingLedge bool to false.

In the LedgeGrab script we will need to return the standing position. It will be a public Vector3 method with the return of _standPos.position. The position will have to be adjusted based on the end of the animation that you have.

Now back in the ClimbUpBehaviour script. We will call the SetPlayerPosition method in the players script.

The last thing we need to do is set the transition duration is set to zero in the transition line, if not it will cause a rebound effect.

That is how we can use the animation event to set the players position to the correct location after the animation. Our climb up animation is now complete.

Thank you for reading and have a wonderful day!



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