# 2D character controller

Howdy ya’ll

So I am working on a 2D character controller and there are a few issues I need to solve with keeping the controller grounded on the surface.

First, some background. I’m not asking how to make the character move up slopes. I’ve solved that with the cross product of the normal and the forward direction. What I need help with is how to keep the player in contact with the ground.

I get that sounds like I should just be using gravity, but I promise you it isn’t that simple (God I wish). So here is what I’m doing:

First, I don’t want to use a capsule collider for the player, I get that they handle slopes better BUT the issue with that is how the handle ledges.

Here you can see how they handle ledges. I want to use a box collider because they won’t dip off the ledge the same way the capsule does AND if I am applying gravity while the collider is grounded (talking about the capsule here) it would slip off. Additionally if I’m on a slope with the capsule and applying gravity I just slide down. I’ve heard some people solved this with by applying an opposing force upwards to the capsule while on a slope but that won’t solve the ledge dip issue. So gravity (while grounded) is out (just setting the gravity scale to 0 when grounded).

Despite the fact capsules handle slopes better, because I am raycasting and getting the normal of the slope the player is on and doing all the good stuff I mentioned earlier, the box collider works great for slopes, the issue is now that I am not staying ON the surface of the slope mainly when I enter/exit a slope. This is because I am raycasting downwards and the cast extends a bit past the collider’s bounds. So what I end up with, and this is evident if I keep running up and down slopes exiting at each end, is a player the eventually hovers above the ground.

When I am checking for ground I am casting from a few points downward. I prioritise the centre point, I figure if I am touching ground there it is a surface far more important to the player. Also if i were to enter a down slope, say I was checking the side points first, it would mean I would try to be moving into the ground as I preemptively try to move down a slope but the body of the collider is still on the flat ground.

So yeah, I have a LOT of stuff happening with this controller. I know the easy solution would be to just remove slopes from the game and save me the headache but I really want them, also it is an awesome way for me to learn.

Basically does anyone know a better way to stay on the ground when moving on slopes or do they know of a good 2D character package they can clue me to (so I can get it and check out their solution) that would be great.

Before I get back to what I’m doing here were some things I thought might work:

1. When on a slope (and only one a slope) raycast form the corner of the collider.

2. Use capsule cast and find the first point the player is in contact with and then do some vector math and move to a position

Thoughts?

Why dont you use your raycast system, and also check a collision for ground? I’m supposing that you set a raycast to X distance. If your raycast are hiting ground, but you aren in collsion with the floor for a Vector3.up normal, then you wont be hitting the floor, so you apply the force. In slpoes, one of your corners will hit it, so its ok if you set certain angle of offset in the check of it, or if you have a slope state, then check a collision. This working with box colliders will work fine with ledges