Skill Level :kinda new (can understand some basics in unityscript).
I am using a 3rd person character controller for my 2d platformer and i’ve been having trouble with slopes. The thing is when i walk down a slope that is approximately 24 degrees it is fine and goes down smoothly, but running down the slope makes it stutter. While not changing any animation (because i lack a fall animation) it signifcantly slows down the player than what it should be.
Now I read that maybe using a raycast thing (i haven’t used these before) would work by shooting a ray a small distance bellow and if it hits than it will say the player is grounded so it wont stutter. Knowing basic logic i don’t think this will work but maybe using the raycast to check the distance bellow the character’s feet will?
If there is anyway to make a smooth walk down any or most slopes please help.
I ran into this problem and Googling for an answer brought up this page. I thought I’d share my answer. The biggest problem I was having is that the player cannot jump mid-air and so would be unable to jump when “hopping” down a slope.
Here is the basic idea:
Apply gravity. In my example, gravity has already been applied.
Run horizontal movement first.
If the player was going to move downward, but not down enough to stick to the slope, then force the player down to just touch the slope.
This means the player will never move completely horizontally off a cliff! But that effect is only for a single frame since I only run this “SafeMove” when walking/running, not when in freefall.
The steepest down slope experienced in our game is a 45 degree angle. If you have shallower or steeper slopes, you will need to change the if statement and what you force the Y movement to be.
/// Walk down slopes safely. Prevents Player from "hopping" down hills.
/// Apply gravity before running this. Should only be used if Player
/// was touching ground on the previous frame.
void SafeMove(Vector2 velocity) {
// X and Z first. We don't want the sloped ground to prevent
// Player from falling enough to touch the ground.
Vector3 displacement;
displacement.x = velocity.x * Time.deltaTime;
displacement.y = 0;
displacement.z = -characterController.transform.position.z;
characterController.Move(displacement);
// Now Y
displacement.y = velocity.y * Time.deltaTime;
// Our steepest down slope is 45 degrees. Force Player to fall at least
// that much so he stays in contact with the ground.
if (-Mathf.Abs(displacement.x) < displacement.y && displacement.y < 0) {
displacement.y = -Mathf.Abs(displacement.x) - 0.001f;
}
displacement.z = 0;
displacement.x = 0;
characterController.Move(displacement);
}