0
\$\begingroup\$

I'm making a 2D platformer game with the default Unity 2D physics. I am implementing slopes, but my player slips down the slope because of the physics.

Character standing on slope

Here is the code I use for the player movement:

MoveInput = Input.GetAxisRaw("Horizontal");
rbd2.velocity = new Vector2(MoveInput * speed, rbd2.velocity.y);
//rotacion de personaje
if (MoveInput > 0)
{
    transform.localScale = new Vector3(1f, transform.localScale.y, transform.localScale.z);            

}
if (MoveInput < 0)
{
    transform.localScale = new Vector3(-1f, transform.localScale.y, transform.localScale.z);            

}
\$\endgroup\$
2
0
\$\begingroup\$

Unity here is constantly running physics simulation (trying to mimic the actual world a bit) and thus applies various forces to your rigidbody2d depending on situation. If rigidbody is on the slope there is the downwards force of gravity applied to it. It results in downwards and backwards velocity so that object starts to slide back.

So your problem is - that this backwards/downwards velocity is greater than your forward/upward velocity which you applied.

We can not change physics algorithm, however we can adjust the parameters.

to fix it (and make it behave the way you want) adjust these parameters:

  • adjust Mass in Rigidbody2d. Try to increase it. This alone should solve the problem.
  • you can lower the slope angle
  • apply higher velocity

It should work.. Also take a look at Rigidbody2D and play around with its parameters until it behaves as you want.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.