BGP2017 – Week 2

Character Controller

To handle the movement of tools and avatars in the game we need a Character Controller. Unity does have a standard component for Character controllers, which is cool. However it still only supports capsule colliders, Which is a problem as we have a stretcher and an axe which would not work with capsule colliders.

Now, there are two options: Either you go for a Rigid body controller or you go for a custom character controller with support for a Box collider.

As rigid bodies are hard to control and there are various bugs in Unity regarding restricting their rotation and movement, it would take a lot of effort and time to pull off, I decided to go with a custom character controller.

So for our character controller we need collision detection and response, gravity and slope climbing.

Implementation

For detection, Physics.OverlapBox() and Physics.OverlapSphere() returns all colliders which the overlap box collides with. When we have the colliders we can resolve the collision by finding the closest point of the surface of each collider and move away from it.

For gravity, by adding a down vector each time the character controller is updated you get gravity. To stop the controller from moving through the ground you want to know when the character is on the ground. This is achieved by using a SphereCast, If the sphere hits a collider on the layer ”Ground”, the character is grounded, otherwise the character is in the air.

And lastly, slope climbing is achieved by using the terrain data of our terrain, namely the interpolated normal of the current position on the terrain as well as the height of the terrain at the current position.

Using these values you limit the slope and figure out the direction the character controller needs to move to compensate for the slope climbing being successful or not.

About Martin Carlsson

2015 Programming