Super Mario Galaxy has a unique character control system where players can move in all directions. It means that characters can move on the wall seamlessly from the floor and vice versa. Gravity is not specific like in other games where the character always falls down. Mario can jump from one mini world to another mini world and start walking right away.
We will learn how to achieve such features in our game using the Unity engine. This should be fairly easy to understand and easily transferable to other game engines of your choice.
What Are We Making?
- Make a simple gravity system so that player can fall when he jumps.
- The player can move in any direction of his choice.
- The player should be able to jump regardless of where he is standing right now.
- The player should be able to jump from one mini world to another mini world and should be able to move there.
Step 1: Creating Gravity
To create gravity, we must first know where the player will fall. Gravity is a force in a certain direction (usually towards the object's centre). But in our case, we won't be applying force toward the centre of an object. In our case, we will apply force opposite to the surface normal of an object.
A Normal describes the orientation of a geometric object. It shows where the surface is facing. So first, we calculate the normal of an object. The steps taken to calculate normal are as follows.
- First, user OverlapSphere to find all overlapping colliders in a certain radius.
- Find a collider with the shortest distance from the player.
- Cast raycast towards shortest distance collider.
- Find Normal at a point where raycast hit.
- Linearly interpolate from current normal to newfound normal to make a smooth transition in current player rotation.
Once the normal is calculated, we just apply force opposite the current normal and have our gravity. Next, we apply rotation to this player using calculated normal so that player always aligns properly on the surface. This will ensure that players always rotate properly when we jump from one world to another or walk on different surfaces. Let's see how we can do all this on code.
Step 2: Creating Player Controls
Once gravity is done, our next job is to move the player.
Moving a player is fairly an easy process. We take input and then use vertical input to move a player in a current forward direction of this player model and horizontal input to rotate the player's model.
For jumping, we just apply instant force towards the current normal direction so that jump is always perfect wherever we are standing right now. Let's look at the code to get a better idea.
Now let's look at how my character is set up in the Unity Editor.
You can see a demo of the final result in the video below!
However, this tutorial doesn't include a camera control system, so that's one part you will have to do on your own. This system works properly as long as the edge is not too steep. It also works on steep surfaces, but you won't get a smooth transition when changing the angle.
And that's it! This wraps up our article. I hope you get an idea of how this system works and can make an even better system than this.
Thank you for following the article. I'll be writing more. Stay tuned.