0
\$\begingroup\$

I'm struggling with a wee issue where if my character walks up a slope he slides back down when at rest, and bounces down when running down the slope. I've followed a few videos but none seem to address the issue. I've posted my movement code so far and I'm not opposed to fundamentally changing this, however with the other aspects of my game, the rigid body and collider setup seems to be working quite well. Any ideas?

 //inputs
    if (Input.GetKey(buttonKey["Left"]))
    {
        inputHorizontal = -1;
    }
    else if (Input.GetKey(buttonKey["Right"]))
    {
        inputHorizontal = 1;
    }
    else
    {
        inputHorizontal = 0;
    }

    //jump
    if (Input.GetKey(buttonKey["Jump"]) && isgrounded.Grounded && canJump)
    {
        jump();
        jumpTimerCurrent = 0;
        canJump = false;
    }

    if (jumpTimerCurrent <= jumpTimerReset)
    {
        jumpTimerCurrent += Time.fixedDeltaTime;
    }
    else
    {
        canJump = true;
    }

 void FixedUpdate()
{
    rb.velocity = new Vector2(inputHorizontal * Time.fixedDeltaTime * runSpeed, rb.velocity.y);
}

    void jump()
{
    rb.velocity = new Vector2(rb.velocity.x, 0.0f);
    rb.AddForce(Vector2.up * jumpForce, ForceMode.Force);
}
\$\endgroup\$
1
  • \$\begingroup\$ If you are new to Unity and/or coding it may be a bit of a learning curve but I believe that most player controlled characters feel a lot better when they are setup as kinematic rigidbodies and controlled "directly" using velocity. Dynamic rigidbodies are truer to real physics (of objects with no force of their own) and easier to setup but I really think that it does not translate well into how we feel when moving (and exercising force). \$\endgroup\$
    – Nikaas
    Commented Jul 26, 2022 at 5:34

3 Answers 3

0
\$\begingroup\$

Have you tried playing around with Physics Materials before. If you add a roughed up Physics Material it might prevent it from sliding back down when not moving. Another trick you could do is just set freeze position and rotation bools of rb when in rest mode. Also by freeze rotation of rb there's a good chance the bouncing might stop. The bouncing might dissappear if you use a smooth phsyics material when coming down. But its weird I can't see why your object would be sliding back down when you clearly are zeroing out the rb.velocity, which should do the trick unless your rb.velocity.y needs to be zeroed out as well which is possible.

\$\endgroup\$
1
  • \$\begingroup\$ I only 0 the y velocity when jumping, I've had a play around with the physics materials and would like to keep it frictionless so that when the collider hits a surface it doesn't stick which was an issue early on \$\endgroup\$
    – Sam Law
    Commented Oct 9, 2021 at 10:23
0
\$\begingroup\$

I don't know why it would slide back down, but the reason it's bouncing down slopes is most likely the fact you're setting its y velocity to 0 when grounded. Try something in the negatives, like -2.

\$\endgroup\$
0
\$\begingroup\$

Dynamic Rigidbodies act like real-life physics. Imagine you had a box on a slope: It would slide down. The same thing happens when you have a dynamic Rigidbody on your player.

These are some solutions:

  1. Set the gravity to 0 when on a slope (done by raycasting)
  2. Change Rigidbody to kinematic if no input pressed / velocity is slow enough
  3. Maybe enablimg freezeRotation can help, or angleDrag (Drag for rotations, not sure how it is called in Unity.
  4. Use Unity's Character Controller component

The bounce when walking back down is an easy fix. Basically your player's velocity is reset when it toucjes the slope, and if the player is fast enough, it will travel off the slope. To fix, just set the vertical velocity when touching a slope (else walking off edges was weird, do with raycast) to something like -2

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .