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:
When on a slope (and only one a slope) raycast form the corner of the collider.
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