Creating a Ledge Grab in Unity (2.5D Platformer) (2) — Climbing Up
Let’s finish up this ledge climbing mechanic by addressing the ‘press E to climb’ logic 😉
1. In our Player class, add an if statement to check for input on the ‘E’ key, and trigger a ‘ClimbUp’ (trigger parameter) on our Animator.
2. In our AnimatorController, add in a ‘Climb Up’ animation state (imported in the same way we imported the previous animations). Ensure this animation has ‘Bake into Pose’ set to true for their rotation, position (Y) and position (XY). Then create a connection between our ‘Hanging Idle’ animation state and this one and add a new ‘ClimbUp’ trigger condition to the transition. Ensure ‘Has Exit Time’ is off for this, as we want the animation to trigger instantaneously when the player hits the E key.
3. Next, create another transition between this Climb Up animation state and our Idle state (ensuring ‘Has Exit Time’ is true for this one).
4. With our Climb Up animation state selected, take a look in the Inspector and click ‘Add Behaviour’. Type in something like ‘ClimbUpBehaviour’ to create a new behaviour script of that name.
Open this script up and you may notice it’s a fair bit different to one of our standard Unity classes (which normally derive from Monobehaviour). The built-in methods for this kind of script are commented out, and the main one we’re interested in is the ‘OnStateExit’ method. Uncomment this out and fill in the code to get a reference to our Player and trigger a ‘ClimbUp’ method on it.
Essentially, this code will get called when we leave the Animation State this behaviour is assigned to (i.e. when the player has finished their climb animation and is transitioning back into Idle).
5. Back over in our Player class, we’ll need to create a public ClimbUp method. I’ve written this to change our Player’s transform.position according to whether they’re climbing up and to the left or up and to the right. I’m also re-enabling the character controller, setting the “LedgeGrab” animation boolean to false, setting our current ledge to null (not that I’m using this variable at the moment), and setting ledgeGrabbing to false.
For checking which direction we’re facing, I’m using a private method with a boolean return type, which checks to see whether our model’s local Euler angles.y are equal to 0 (i.e. is facing right perfectly) or not (i.e. is facing left, or, technically, is facing anywhere that isn’t perfectly right).
With this all done, we should have a working ledge climbing system which lets us trigger a climbing animation then immediately change the actual position of the player accordingly (otherwise, as soon as we go back to Idle, we’d see the player’s still exactly where they were hanging). This should create the illusion of the player’s position changing in realtime/in time with the climb up animation.
Lastly, resetting a couple of variables and setting our character controller back on should fully restore control to the player so they can be on their way 😉