Making Climbing/Vaulting System in Unity
In this blog, we will be creating climbing system in unity. We will be using unity’s character controller and animation rigging. Considering that you have your character set-up, or you can start from Starter Assets.
Now a climbing system can be broken into 3 steps.
- Detecting height of the Obstacle and Hand Placement.
- Handling and playing the vault animation, with Animation Rigging
- Transition back to normal start
1.Detecting height of the Obstacle and Hand Placement
To detect the height of the obstacle. Here we are applying a line of raycasts towards the forward vector of the character.
Here rendering space separated raycasts that detect whether there is an obstacle or not.
We are rendering from the parent object i.e. Scanner_Left and Scanner_Right.
The Scanner_Right and Scanner_Left symbolize the right and left positions of the hand placement.
Vault_RayCastGen.cs is handling raycast object rendering. Let us look at its code.
Now each raycast object has a VaultCheckController.cs
Lets look into this code.
Now our obstacle detection is ready. We just have to find the height!
To find the height of the for the obstacle we can run a loop on the instantiated raycasts to find the first raycast with ‘isVaultable = true’ in Vault_RayCastGen.cs.
Now we have all the vault_position for Scanner_Right and Scanner_Left respectively.
Finally, We encapsulate with VaultHandler.cs that handles the gizmos rendering of the hand placement position and position for the character after climb.
And we are done!
2. Handling and playing the Vault Animation.
In this section, we will handle handling and executing our vaulting/climbing animation.
Since the height is dynamic, one way is to use a BlendTree.
We will applying the VaultHeight to the height we calculated in
Section 1 (VaultHandler.cs).
To ensure proper hand placement we need to apply Animation Rigging.
In a nutshell, Animation Rigging makes your animations reactive.
For more info you can watch this video.
Now now we will just fix the animation rigging constraint to the Hand positions calculated in Section 1(Vault_RayCastGen.cs).
Now we just have to setup input system. That’s pretty straightforward so I’ll continue with the onClimbCheck() function.
And we are done with the Animation Part too!
3. Transition back to normal start
Note, we don’t move our character controller during our animation. So we have to shift our controller up when the animation ends
So in the end we have to add an Animation Event. that Triggers a function. that handles the character movement.
onClimbEnd() function.
And Finally we are done!