Comparison between Character Controller and Rigid body in Unity 3D (Part 1)

Tram Ho

Foreword

Not only in Unity but in any engine, the Character controller is one of the factors that most impact the user experience when playing your game. If you don’t take care of it, your game will almost certainly fail, no matter what other aspects are invested. However, creating a good Character controller is not easy either. When you want to create a character controller, there are a lot of things you need to consider, from processing player input to turning those inputs into motion in your game.

One of the first questions you should ask yourself when thinking of creating motion for your game is: Should I use a Character controller or a Rigid body?

In this series, we and we will try to analyze and compare a little bit of these two approaches by doing some simple running and jumping movements for the character. Hopefully after this series, you will have a better understanding of the Character controller and Rigid body in Unity, thereby choosing the most appropriate method for your game.

How to use the Character controller

Setup

Create a project with a normal test scene, your project should have this structure to be more manageable.

Then create a scene of your choice (note the need for a floor) to test the movement of our characters in it. For example, here I created a scene called the Character controller with stairs to perform jump motions and use a simple capsule to act as the character to control.

Your screen should look like this, use parent gameObject to make it easier to control your scene.

Character Controller

The character controller is a component in Unity that you can assign to the gameObject you want to control. Its effect is to move gameObject in scence environment. It is not affected by or affecting physical features.

The Character controller also comes with an Capsule Collider . Before you get started, it’s a good idea to read Unity’s guide to Character controllers and scripting APIs .

In this example, I use the default paramenter but you are free to adjust them to suit your game.

The core principle of the Character Controller is that it provides a basic collier without any physical properties. Basically you will move your player the same way you use Transforms , but you cannot go through the collider. The advantage of this method is that we can better control the movement of our players. But the downside of that is that you’ll almost have to code everything.

The Character Controller has two methods for moving players: SimpleMove and Move .

SimpleMove takes speed as a parameter and will move the player based on it. The player will be affected by gravity, which is the only physical effect that the Character controller provides. The minus point is that this method will ignore speed in the Y axis.

Move will require you to handle more but you will be less restricted. It takes absolute motion, so you’ll have to handle gravity with your code yourself. It was more laborious, but it was the method I often used because SimpleMove could not control the Y-axis movement.

The basic movements

Try implementing some simple moves. First, create a new C # script named “Character” and assign it to our player. After that, we will need a public variable so we can adjust the speed directly from the editor and a private variable to store information about our Character controller.

In the Update function, we take the inputs, then save them to a vector3. We then call the Move function and pass in our vector, multiplying the speed and DeltaTime so that the player’s motion is independent of the framerate. That’s it, we have our basic movement.

Gravitation

Let’s add gravity to increase the authenticity of the movement. First we will need a variable that represents gravity (or you can use the default variable of Unity – Physics.gravity.y) and a private variable to save the player’s speed.

Then we just need to add gravity to the player’s speed with the update and execute that speed with the Move function

If you try and run this code, you may notice our gravity is a bit strange. That’s because even when the player touches the ground, the velocity increases due to the influence of gravity. To solve this problem, we can reset the speed to 0 when the player has touched the ground.

CharacterController already has a variable to determine if the player has touched the ground, but it is quite unstable so I decided to handle this problem myself. I like to use the CheckSphere method in the Physics class. This method will return true if our sphere collides with some collider. Let’s create a blank gameObject in the middle of our player as the center of the sphere and use a variable to adjust the radius of that sphere. This way we can adjust the collision radius from the editor.

Here, _isGrounded is a bool variable created in the class and _groundChecker is the center of the sphere collider. GroundDistance is the radius of sphere and Ground is the layer containing the floor.

Dance

Making a jump is actually quite easy. When the jump button is pressed and the player is on the ground, we change the speed along the player’s Y axis. We can use JumpForce to control the jump in this simple formula:

or if you like a little bit more complexity so we can control our jump better, including the height of the jump.

Surf and brake

Another example of motion you can use in your game is surfing. When you press the surf button, the player will move in a certain direction at a faster speed than usual. As with jumping, you can use direct or an algorithm to control it better. The algorithm here is a bit more complicated and after a few experiments I have found this to be the best way.

In this code we will increase the player’s movement vector in a certain direction. Vector surfing will depend on two variables: surf distance and drag. We need resistance, otherwise the player will rush away forever. This resistance is to simulate actual friction (friction with air or ground). Resistance is a value that has a value between 0 and infinity, with 0 being a complete zero resistance level. Here I have chosen a Vector3 to represent drag, thereby controlling drag on all three axes. Finally, to apply this drag on our player, just add this code below the Update function:

Conclusion on the Character controller

After trying to perform a few basic movements in Unity using the Character controller, you probably also recognize the pros and cons of this method. The character controller gives us a lot of freedom to customize our movements, but the downside of that is we will have to code almost everything, including the most basic movements like gravity or jumping. .

This conclusion is also the conclusion of part 1 of this series. In Part 2, we will also perform some basic movements in Unity, but using the Rigid body. We will then comment on and compare the advantages and disadvantages of these two approaches.

Thank you for taking the time to read this article and see you again in part 2!

Share the news now

Source : Viblo