# Keep same speed on straight segments and slopes.

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

## Recommended Posts

Hi,

I'm using unity3d for my upcoming game. I'm trying to create a little platformer using the built-in physics engine ( PhysX) but I'm having problems with the character controller. I know that unity has a built in character controller but it doesn't fit my needs. I want to create simple old-school 2d plat former mechanics.

One of the problem I'm facing right now is to keep the speed on straight segments and slopes constant.

I'm applying a constant force to my rigid body but when it starts going up a slope it decreases its speed and when it is going down a slope it increases its speed relative to the speed it has when on a straight segment.

I have seen a lot of games with slopes and they don't seem to have any problem with it. It seemed a simple thing, but it seems it isn't so obvious.

For my player I'm using a sphere collider to allow it to travel on slopes.

If anyone needs any more information let me know and I would be really pleased to provide it.

##### Share on other sites
I found a pretty good solution for projecting motion onto a slope. My problem was in 3d, i wanted the character to cover the same amount of surface distance no matter the slope of the terrain.

For that i took his original horizontal input velocity vector and i shot a ray down from the tip of it into the plane that he was standing on. (not a real raycast, but just the "find t" equation for a ray and a plane.

I then constructed his projected velocity from the ray intersection point back to his origin, and set its length to the magnitude of the original input.

Then, no matter what slope, up down left right, he was running on, he always ran straight and covered the same distances on the surface.

http://en.wikipedia.org/wiki/Line-plane_intersection

The plane is instantaneous derivative of the surface you are standing on, and the ray is in the world -Y direction, starting at the tip of your original velocity, based at your origin.

If you are literally just applying a force, and not considering your current velocity, then you will need to a least consider the acceleration from gravity. If your slop was nearly vertical, you would need to apply a lot of extra force to combat gravity. I would suggest against using forces. My controller takes a "desired translation" and resolves it against collision constraints before applying it to the body.

##### Share on other sites

I would suggest against using forces. My controller takes a "desired translation" and resolves it against collision constraints before applying it to the body.

So, you do the collision detection by yourself?.

##### Share on other sites
The controller is part of the physics system, and runs after contacts have been found, but before objects move. so it clamps the desired translation based on contacts and then applies the remaining translation when it's finished.

##### Share on other sites
Ok, thanks for the suggestion .

##### Share on other sites
Just use the surface normal (which is precomputed, or can be fast bilinearly interpolated, for a triangle mesh) and dot it with your character's "up vector" to find the angle of inclination. Then use that to split your velocity correctly into vertical and horizontal components.
In fact, usually you have velocity in vector form anyway.
You find your new position by simply computing old_pos + t * velocity, where t is determined by speed and time step.

##### Share on other sites
Domii, what if you're running along the side of a hill, and your inclination is computed to be 45 degrees? your velocity inclination should be zero.

If you were running straight up or down the hill, your method would work, but that is rarely the case. Dotting the velocity direction against the surface normal may do something. In fact it may turn out to be exactly the same math as what i suggested.

• ### 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!

• 15
• 12
• 9
• 11
• 15