- Home /
Grounded check not working on sloped ground.
Hello
In building a platformer in Unity2D, I've come across an issue. I've set up a basic check to see if my character is on the ground, where it sends a linecast from the player object to an empty child object below it and checks if the hit is a ground tag. The trouble I'm having is with sloped ground. From what I can tell, it isn't long enough to reach the slope from the angle, making it constantly show the falling animation and making the character unable to jump. I don't really want to extend it too much because a noticeable height in jumping just above the ground may appear. I'm not sure what else to do. Is there a better way to check if the character is grounded?
I can provide anything needed, like code or screenshots.
You could try using more than 2 linecasts and the player is grounded if at least one of them says it is. Since the ground is sloped it will be futher bellow the player on one side (where the linecast would say it's not grounded), but on the other side of the player the slope wouldn't be as far bellow so a linecast on this side would work.
Oh wow, Can't believe I didn't think of that. Thanks! It works now.
Answer by DawdleDev · Apr 30, 2018 at 01:50 PM
Here's an idea. Use three raycasts: one straight down and two at perpendicular angles to the slopes. Then, you can use all of them to check for the slopes on top of the ground. Another way to fix this would be to angle the raycast based on the player's direction of movement (IE moving up a slope), but this is more buggy. A third and final idea is to use a trigger under the player's feet that's the width of the player and about as tall as your raycast, and use OnTriggerEnter to handle the jumping. You could do this in a child object if you need to (IE if you have a trigger on the player).
Hope this helps!