Creating the Ledge Grab System (part 2)

Scripting the player to defy gravity and hang in place.

Simon Truong
4 min readOct 6, 2021

--

Continuing from the previous article, we have two checker box, one for the player and one for the edge of the platform. Now we need to script the player to freeze the gravity so they don’t fall during the ledge grab session and also toggle the hanging animation.

Preparing for the ledge Grab:

First we need to add an “Rigidbody” to the player’s ledge grab checker. Then we will need to create an new Tag to categorize the all the current and future ledge checker, then assign the player ledge grab checker to that tag.

Creating a new tag for the player ledge grab

Scripting the Ledge Grab:

Ledge Script:

The logic behind the ledge grab is quite simple, we will need the player script to disable the character controller when the two box checkers collide with each other, then we can at the same time, toggle the animation to hang.

Start by creating a brand new C# script called “Ledge behavior” that will be attached to the ledge checker. Inside the script we will use the OnTriggerEnter method to check whenever the box colliders have made contact, and then they they do make contact, we need to have a script communication between the Player script and the Ledge Grab script to turn off the Character Controller.

To make the reference to the player script and since the player script is on the parent object, we need to use “transform.parent” before we can get the component. Of course due to best practices we need to do a null check before we can proceed to add more to the script. In the null check, we will trigger the public method (which we haven’t created yet) from the player script which will stop all functions from the Character controller.

Player Script:

In the player script, we will need to refine some of the scripts inside to keep organized. First copy and paste all the movement calculations into a individual method called “Calculate Movement”. Make sure to call the new method within the update method, now that we have removed the script from it.

Created a new method for the movement calculations to keep the Update Method clean

Second we need to create the new public method that stops all the functions from the Character Controller. Since we called the method “LedgeGrab” in the other script, we need to follow the same naming convention here to keep consistency.

In the method, we will disable the character controller (which we gave the variable name “controller”) by setting the enable to be false. Then accessing the animator, we will use the Set Bool for the Ledge grab to true (which we haven’t done yet) so it will meet the conditions and toggle the hanging animation.

Setting Up the Animation:

Back in Unity Animator, we will need to deleted the old Trigger parameter and create a new Bool type parameter to toggle the hanging state. Also we will link the hanging state from the Running jump state since that is when the player would reach the hanging animation the most.

Finally in the Conditions tab, we want to set the “has Exit time” to be unchecked and have the conditions to be set to the “LedgeGrab” boolean. Then set the boolean to be “true” so ONLY when the the boolean is set to true will the animation be triggered.

Now when you play the scene, you will noticed that the player will stop the character controller as it makes contact with the ledge, meaning that gravity is also stopped.

In the next article we will focus on setting the player to snap into a preset position and trigger the climbing up animation.

--

--

Simon Truong
Simon Truong

Written by Simon Truong

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.

No responses yet