Working with Animation Events
Getting the Player to teleport/snap into position while going into the Idle state.
Pressing E button to toggle Climb Animation:
Lets get the first action to get out of the way, pressing the E button to trigger the Climbing up animation. In the player script, since the Character Controller is currently deactivated during the ledge grab, we need to place all the scripts on the Update method.
To being, we need to tell create the logic so Unity knows that we are currently on the Ledge, therefore we can use an Bool system. In the public method “Ledge Grab” set the on Ledge bool to be true.
Then on the Update method, we use an IF statement to set the conditions to play the climbing up animation. First we check whenever the on Ledge bool is set to true, then we check if the “E” button has been presssed. If those two conditions have been met, toggle the climbing animation.
Now if you hit the E button while hanging, the animation for the climbing should work.
Working with Animation Events:
After the Climbing animation is finished we want the player to revert back to the idle state, but noticed that our root location is still on the ledge so when the idle animation plays it teleports the player back to the side.
To remedy this is where the Animation Events come into play. Animation Events is basically special scripts that are linked to the animation state of the Animator. Similar to how we have the On Trigger Events (Enter/Stay/Exit). The Animation Events have something similar. There are event triggers that happen during the enter of the animation state, stay(which is during the duration of the animation state, and exit time of the animation state).
To create an Animation Behavior script, select the animation state (in our case, it shall be the Climbing state) and In the inspector click “Add Behavior”. The script when opened, should have everything in green, what we want to focus on is the 3rd section (or line 19) which is the On state Exit (similar to On Trigger Exit). What we want is to trigger the snapping of position just as we exit the hanging state, and to get all the data together will need some serious script communications.
What we need is for the Ledge Checker script that stores the Vector3 Position data to communicate with the Player script so the Player script can piece together the animation and the Animator. At the same time, we also want the Animation Even script to take the stored position data from the player script to trigger the precision snapping of the position just as the animation state is finished.
Although it sounds confusing, we shall complete these step by step so it will become clear overtime.
Ledge Checker Script:
First we need to set a new variable from the Ledge checker script to house the position data.
Then I make a public method with just an return script to pass the vector3 data to the other scripts.
Finally on the On Trigger event method, I added just one word “this” in the player reference.
In the player script, we need to match the variables set in the ledger script, since we added an new variable “standingPos” we also need a new Vector3 variable on the player script to house the “standingPos” data.
In the public method “LedgeGrab” we need to incorporate the “standingPos” vector3 data and convert it to a Player script vector3 data. Basically:
LedgeGrab Vector3 = Player Vector3
Now that we assigned the names to the Vector3 data, we need to make a public method (which the Animation Event script can access) which converts the LedgeChecker’s position data to our player position.
At the same time, we also set the Ledge grab animation parameter back to false so it turns off. Then also enable the Character Controller so we can move around again.
Animation Event Script:
Now that we have converted the data from the Ledge checker script into the Player script and created the public method which houses the snapping of positions of the player (along with enabling the character controller) we need to toggle the method at the precise moment the Climbing state is finished.
In the Animation Event script, you can activate the “OnStateExit” section by deleted the “//”.
We are going to give the variable Player player so we can get the access of the Player game object which houses the Player script. Since it is a parent script we need to use the Transform.parent line.
Do a null check and only when the player has been assigned we trigger the public method from the player script “ClimbUpComplete”.
Save and return back to Unity, once you play the scene your character should automatically return back to idle state while your root position has been snapped to the top of the platform. If you see that your player dips below the platform for a few seconds, make sure that the Animator transition duration is set to Zero. That would make the transition between states instant which prevents awkward dipping and jerking of animation states.