itch.io is community of indie game creators and players

Devlogs

One of the problems with Unity's CharacterController, and how we solved it

Iconoclast
A downloadable game for Windows

Hi everyone, Hunter here, Gameplay Programmer on Iconoclast. I wanted to talk a bit this week about a specific issue we ran into while making Iconoclast, and how we resolved it. Warning: technical jargon ahead!

Unity's built-in CharacterController component can be very useful for setting up player movement in a 3D game; it automatically generates a capsule-shaped collider, it has built-in properties for things like detecting whether it's touching the ground, or determining what part of the player is colliding with something, and things like the player's step height and the maximum steepness of slopes they can climb can be easily set in the inspector.

 But one major caveat to keep in mind when using the CharacterController is that it doesn't interact with Unity's built-in physics system. This means that by default, a player with a CharacterController won't be effected by gravity, react to forces, or generate collision events. The lack of collision events presented a problem while developing Iconoclast.

The CharacterController does, in fact, have a built-in function for detecting collisions. OnControllerColliderHit is called whenever a game object with a CharacterController collides with something. However, OnControllerColliderHit is only called while the player is moving, and it gets called over and over continuously if the player stays in contact with an object. For these reasons, OnControllerColliderHit wasn't going to cut it for our purposes. If we wanted to, for example, play a sound effect when the player bumps into something, we couldn't use this function because it would play the sound effect repeatedly as long as the player is touching the object.

I was able to create a workaround for this issue using the following steps:

First, I drew a capsule slightly larger than the CharacterController's built-in capsule collider each frame, using Physics.OverlapCapsule. I assigned the function's results to a collider array, which I then assigned to a global list variable called lastCollided:

Download Iconoclast
Leave a comment