Should we use a RigidBody, Unity’s built
in Character Controller, or make our own to move our characters? The answer to this
question is honestly on a per-game basis, but to make the right decision for your game,
it’s helpful to have a solid understanding of each choice. Today we are going to learn
fundamental concepts of Character Controllers! This way you can decide which makes
the most sense for your game!
Welcome to iHeartGameDev, my channel all
about game development. If you do enjoy today’s video please be sure to like and
subscribe. And now, let’s get started!
For today’s project, we have an isometric scene
with a character gameobject. The child of this gameobject is the downloaded Jammo character
from the hit youtube channel “MixAndJam”. Jammo has animation states for standing, walking
and running and a script that will handle the blend between these animation states. Ok!
Now, on to Character Controllers!
Let’s begin by simply defining what
a “Character Controller” actually is. This will ensure that we are all on the same
page and hopefully help alleviate confusion.
A character controller is a component (or
combination of components) attached to our character gameobject that serves the purpose
of providing movement to our characters and interaction with colliders
in the game -- be it the floor, walls, stairs, other characters, projectiles, etc. And that’s it: at its core, a basic character
controller provides movement and collision handling. Now, let’s get an overview of our
options for character controllers in Unity.
When attempting to learn about Character
Controllers, the first search result will lead us to Unity’s built-In
character controller component. This pre-made character controller provides
our selected gameobject with a collider, and access to a list of methods and properties which help provide standard
features for characters.
If we were to break the built-in character
controller into separate components, we could say it’s basically a collider component
and a script written by developers at Unity. This script contains the built-in Character
Controller class, a wrapper around Unity’s PhysX engine’s character controller. This
class allows us to access the list of methods and properties previously mentioned inside
the Unity editor and in our own script.
With methods for character movement,
the ability to walk on uneven ground, up and down slopes, built-in collision-detection
and more: this character Controller includes features we typically look for
in character movement. Therefore, depending on our needs for our character
in our game, using the built-in character controller may be the fastest implementation
for setting up character movement.
If you are interested in a complete explanation
and break down of each property and method, let me know in the comments and I
will cover it in the future!
But for now, it’s important for us to remember
that the built-in component is a just single example of what a character controller can be,
and it is not the only possible option.
We can also get our character moving with
a rigidbody-based character controller. Rigidbodies are components that allow gameobjects
to interact with physics in real-time. Unity’s physics system includes properties like gravity,
mass, drag, momentum, and more. So by adding a rigidbody component to our character, we are
essentially telling Unity we want our character to be included in the physics system and all
of the physics forces to be applied by default. Additionally, if we attach a collider to our
character gameobject, the physics system will handle collision detection against most other
game objects that have colliders, as well.
When a rigidbody is applied to our game
object, it inherits all of the methods and properties of the Rigidbody Class, including
multiple functions that impact movement! This is similar to what we were just discussing
with the built-in CharacterController: how the gameobject that has the
built-in component can access a list of its own methods and properties.
But the list accessible from Rigidbody class is completely different than
the list from the Built-In Class.
One of the key properties of rigidbodies is
the “isKinematic” property. When checked our Rigidbody is considered “Kinematic'' and when
unchecked our Rigidbody is considered “Dynamic”. Kinematic disregards incoming physics
forces from the gameobject, like gravity. And the kinematic gameobject isn’t moved by
collisions by default. The isKinematic property essentially tells Unity that the gameobject
isn’t static, so it is intended to move. And that we want the benefit of collision
detection, but we will handle how it moves and how it reacts to collisions with our own code!
Dynamic Rigidbody movement, on the other hand, is physics-based and therefore intended to
abide by the “laws” of Unity’s physics engine. The available functions for rigidbody movement
will apply different forces to the gameobject: such as applying an impulse force on a bullet,
gravity to a ball, or velocity to our character. Dynamic Rigidbody gameobjects will also
automatically be impacted by other forces, like when Jammo is hit by this launched cube and how
it will not walk through this static wall.
Again, if you are interested in a
deeper breakdown of the entire list of methods and properties accessible for
Rigidbodies, let me know in the comments and I’ll make it happen in the future!
Let’s do a quick overview comparison between the candidates we have so far: the built-in
character controller, the dynamic-rigidbody and the kinematic-rigidbody character controllers.
The standout difference is that both rigidbodies inherently interact with physics, while
the built-in character controller does not. By default, dynamic rigidbodies directly
impact and are impacted by all physics forces, while kinematic rigidbodies only impact other
dynamic rigidbody gameobjects -- they are not impacted by dynamic or kinematics
rigidbody gameobjects.
Therefore the built-in character controller
and kinematic rigidbody character controller will not be affected by physics forces, and
only moves when and how our code tells it to. As a result, gravity isn’t automatically applied
to the gameobject, there is no momentum for the movement, and there is no outside force that
will automatically slow down our character. Instead we need to program all of this ourselves.
Although Unity’s developers did create a method called SimpleMove for the built-in character
controller which applies gravity by default.
The built-in character controller also can move
up and down slopes of set degree angles and is able to handle stairs. But, the built-in won’t
automatically slide down slopes if the character lands on, or stops in-place, on a highly-angled
slope. Meanwhile, a dynamic rigidbody will slide down slopes by default, but will not stop in
place without the right amount of drag and it doesn’t properly handle steps the same way
the built-in character controller does.
The built-in character controller also has
a property which detects if it is touching the ground, while a dynamic rigidbody
has no such property. Again, this can be programmed into a rigidbody character
controller without too much trouble, but it’s worth noting because it’s a
property we often need for our character, especially if it is intended
to have a jump mechanic.
Can you see the trend here? In general, neither
the built-in or the dynamic rigidbody-based character controller is feature complete by
default. While there is always a chance that the style and complexity of our game won’t require any
adjustments to the built-in character controller, and there’s an even slimmer chance that
we won’t have to add new properties for a dynamic rigidbody character controller; we
will typically need to program some of our own modifications to get our final character
controllers working just how we want them to. And at the bare minimum, we are required to
handle the movement script ourselves.
This leads us to our last options for character
controllers, outside of downloading one directly from a package or asset store. A Kinematic
Rigidbody would need to handle any physics reactions and any additional features that
the built-in has by default. Or from an even blanker canvas, a completely custom character
controller would require an extensive script to handle pretty much any of the features that
are included by default for dynamic rigidbody and the built in character controllers. For
this reason, a completely custom character controller is the most complex option but
the most flexible, because it can function exactly how we want as long as we know how to
program the features we’re looking for.
Now, in a turn-based JRPG like pokemon, it’s
highly unlikely that we would need rigidbody physics and the built-in character controller has
most of the features the characters need already available. But in a game like Fall Guys, the
entire game is based off crazy scenarios caused by the physics system, it needs some interaction
with physics and Rigidbodies are the way to go! To extend what was explained at the start
of this video, we’ll need to have a solid understanding of the features our characters
require and which character controllers can best provide those featuress to ultimately choose
the right character controller for our games.
I do want to mention, because it’s worth
noting, that there is absolutely nothing wrong with purchasing pre-made character controllers
from the asset store. At the end of the day, our time is the most valuable resource
we have. If we are more interested in getting a game up and running, finishing
and releasing the title: whatever gets us there fastest and most efficiently is likely the best
option. We may be able to save ourselves time, effort and confusion through someone
else’s hard work. But with that being said, this series is dedicated to learning the ins
and outs of character movement on our own.
Next time, we’ll take a deeper look at programming
movement with each type of character controller. And soon we’ll start learning how to use
the suite of components provided with Unity’s animation rigging package. Be sure
to subscribe and hit that notification bell so you never miss a video/release! We’d love
to have you in the channel discord, and if you want updates on the next video, feel free to
follow me on twitter. But that’s all for today, thank you so much for watching
and I will see you… next time!