I’m currently making a 2d platformer, and I’m having some problem handling slopes.
I’m checking whether my character is on a flat surface or on the platform by using raycast shooting down from the player’s feet position.
It seems to work fine, and nothing goes wrong with it.
But I found few problems and I really couldn’t find any clue to solve these.
First problem is that the characters bounces up when there is a drastic change in the angle.
So I wrote a code like below.
private void FixedUpdate()
{
if (isOnSlope)
{
workSpace.Set(-player.detection.slopePerpNormal.x, -inputX * player.detection.slopePerpNormal.y);
// slopePerpNormal = Vector2.Perpendicular(hit.normal).normalized
// this vector always points to the left side(?) of the slope no matter where you are facing
player.movement.SetVelocity(workSpace, playerData.moveSpeed);
// SetVelocity function sets the player's velocity to given angle and move speed towards his current facing direction
}
else
{
player.movement.SetVelocityX(inputX * playerData.moveSpeed);
player.movement.SetVelocityY(0.0f);
// SetVelocityX/Y simply sets the character's velocity x or y velocity to given value
}
}
It improved the movement a lot and works well in low speed but still sometimes the character bounces off the slope when he is dashing.
Is there any way to perfectly make the character stick to the ground?
Second problem is, when the character jumps on the slope, he slides down a little bit. It changes the velocity to zero when it goes to idle state, but still he slides down. Is there any way that can prevent this from happening?
Thank you in advance.