Posts
Search
Contact
Cookies
About
RSS

Godot - Player collisions

Added 3 Nov 2024, 10:05 a.m. edited 3 Nov 2024, 11:08 a.m.

For basic move and slide collisions adding a simple collision shape such as a capsule to a character body 3D is all you need.

However your first issue comes when you want be able to climb steps, unless your stairs have very low risers then you character is going to come to a halt when it hits a steps riser.

Another issue comes if you wish to handle some types of ground objects differently, for example an electrified floor.

You can add an Area3d to an electrified floor and this will give you signals for different areas, however you then need some mechanism to communicate this to the player script, this can be less than ideal and will often rely on needing you to remember manual additional steps when instancing a scene in your world

Rather than doing this you can add areas to the player itself, you will still need your separate capsule so your player reacts to walls and ground, as while an area3d generates signals on collisions, it won't prevent motion when encountering a wall or even the ground. Ensuring your instanced scenes are in an appropriate group will mean that no extra actions are needed when instancing a scene (adding a new flight of stairs into your world for example)


The ground collider sticks just slightly below the main collider as soon as the player drops or walks onto a body it will generate signals for the player, in the player script we can then check which group the colliding body is in and react accordingly.

Of more interest is the stair collider, this is a cylinder with a smaller radius than the capsule and is offset towards the front of the player, its height is also positioned such that when the player moves forward into a steps riser a signal is generated, in the player script the handler for this area when detecting the stairs group will add an upwards force to the player, just enough to bring the player over the single step.  Because the stair collider only sticks out of the front, then when the player is going downstairs it won't generate signals

The camera handling is worth noting, when the player turns the capsule itself rotates - putting the stair collider etc in the correct direction, however looking up or down, only changes the angle of the camera pivot.

There are obviously other ways of achieving similar results, but I've found this solution to be the most robust and works nicely with the other features of my player.