One of the problems with Unity's CharacterController, and how we solved it
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:
Get Iconoclast
Iconoclast
Break the rules - literally.
Status | Released |
Author | lucasjonesgamedev |
Genre | Platformer, Puzzle |
Tags | 3D, Fantasy, First-Person, Puzzle-Platformer |
More posts
- Worldbuilding - How To Develop Your Game's SettingJul 19, 2023
- The Art of IconoclastJul 19, 2023
- Environments in IconoclastJul 18, 2023
- Adding detail and depth with just texturesJul 07, 2023
- Player Movement RefinementsJul 04, 2023
- Enhancing player experience with VFXJun 27, 2023
- Creating a character from concept art to fully animated!Jun 12, 2023
- Alpha UpdateJun 05, 2023
Leave a comment
Log in with itch.io to leave a comment.