- Home /
2D Slope acceleration
I've been working on a 2D Platformer game in Unity using Raycasts for collision detection. So far everything is working fine, including slope detection. I'm not using any rigidbodies or physics, but I would like to emulate some slope physics. I'm just not sure how to go about doing it.
Basically what I want to do is make it so when the player is running down a slope, they will move faster, and when going up a slope, slower. After running downhill, I'd also like the player to also keep some of that momentum and keep running at that pace for a time. Right now, I have calculated the angle that the player is currently moving on using Raycasts, but I'm a bit confused on where to go from there. I've always been terrible at trigonometry.
Maybe someone can point me in the right direction?
Answer by ViFFeX360 · Oct 24, 2015 at 01:36 PM
Basically, what you'll need to know, is the ground speed. So the speed at which the player is moving while on the ground, regardless of your horizontal or vertical speed.
After that, you'll need to use sin and cos to move up/down ramps.
Example:
horizontalSpeed += groundSpeed * Mathf.Cos(angle);
verticalSpeed += groundSpeed * -Mathf.Sin(angle);
then you'd have to
transform.Translate(horizontalSpeed, verticalSpeed, Time.deltaTime);
This is just pseudo code, but that's how it should work.
Good resource with information on how Sonic the Hedgehog tackled it: SonicPhysicsGuide