# How to calculate character movement on a slope?

Hi All,

I’m working on a [game]

And I’ve got character movement working on flat land but now I’m trying to get movement working on an incline/decline.

So, I’m trying to figure out how to calculate the player’s position on the incline as they move.

The character is currently only moving on the x and z axis. The y axis is up and down.

A simple solution would be to take the player’s next x and z axis (and when I say next I mean as they’re moving, the next position they’ll be in), then create a raycaster from that next position pointing upwards, see where it collides, and then set the player’s y coordinate there. However the issue with that solution is that the player will be moving faster on an incline than on flat land. Due to the fact that they will be moving at the same speed on the x and z axis but will also be moving on the y axis which means more distance traveled in total.

So I’m trying to think of a different way to calculate the player’s position on a slope. I’m thinking I might be able to use a CircleGeometry object to detect the circle’s intersection with the slope to achieve a consistent movement speed. But as far as I know, RayCaster is the only object that can detect intersection.

Has anyone face a similar problem to this?

I’m actually looking for slope detection too so as to use an angle such as 65.0 to detect a steep incline & NOT letting the character climb up it.

Why not just computing a movement factor based on the current and the previous `y` position? You can use it to modulate the final movement speed.

1 Like

I’m confused what you mean here. How would that help me calculate the final position?

The character moves only in the xz plane. You use the result of raycasting to keep the character a defined height/distance above the ground.

1 Like

That might be able to help me calculate movement speed but I don’t think that would help me calculate position. The surface that the character is moving over will be unpredictable so I can’t simply find the next y position based off my previous y position. I think that would only work if the surface that the character is moving over is inclines at a constant rate.

Okay that’s what I was thinking of doing.
So it sounds like you’re saying use the previous y position to get a rough estimate of the speed for the next move.

Correct. Simple, easy to implement and fast.

Cool, I’ll give it a shot, ty!