Public Group

# How to stick on slopes?

This topic is 2151 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So, I was making my first game, and I have encountered a problem I did not expect.

As you can see, instead of staying on the ground, my entity just flies off when it meets a downward slope.

I want the entity to stick to the ground, but I do not want to change its moving and falling speed.

How can I do that?

Edited by Kain5056

##### Share on other sites
This can actually be a difficult problem. The primary guess here is that you are using a physics engine. If so, for the character you might stop using the physics calculations completely, or at least until actual "falling" is supposed to take place. Basically as you move around, simply perform a ray cast to the ground, and place the entity at the appropriate height manually. As you move along though, let's say the left of the hill was a sheer cliff, you would probably want to actually fall off of it at that point instead of snapping to the ground immediately. What you do is set a bool on the character "falling", re-enable the physics as the controller and ignoring your movement code, set the appropriate velocity such that the physics engine will take over. When collision is detected with the ground, turn off physics and go back to the faked movement. For jump and related, the same basic pattern works also, turn off your code, set the physics engine to take over and wait for a collision before enabling your ground movement code again. (Note: obviously you need to detect if the collision is with a walkable surface and not just "any" old collision..)

##### Share on other sites

It appears that your character is working in a low-gravity environment, and he's not sticking to the ground because the speed at which he falls (y) is relatively slow compared with his horizontal speed (x). You could increase the gravity only when there are sloping tiles. Something like:

if tile == slanted && player_movement == walking #(not jumping / airborne)
player.y += 2.0 #high gravity
else
player.y += 0.5 #low gravity
end

That way you'll have the usual low gravity unless he's on a slanted tile. If you post some of your code, or even better, upload to github and post the link, you should be able to get a more detailed answer.

Edited by ml_

##### Share on other sites

I had the same problem and my approach was to suck the character down to the ground when it was touching the ground last frame and <= [some limit] distance from the ground this frame. I set [some limit] based on the max x velocity and max slope of the ground.

##### Share on other sites

You can also track the current position, and instead of moving the character by velocity units, you track along the ground for velocity units (changing direction when a slope is met) and move the character to the position you end up at. If you end up mid-air just activate the falling physics.

This would probably work regardless of character speed (the character can follow a complex terrain through the whole level in one frame if needed...) but will of course make it harder to add realistic physics since you pretty much have to implement them yourself (unless you can easily switch between this and real physics every time something happens)

It would give a good base for pathfinding though.

##### Share on other sites

It seems to me like the top block has a slight bump at the left of the top edge... If so, that might be what's propelling the character upwards a bit. :)

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 11
• 9
• 24
• 48