0
\$\begingroup\$

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\$
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
    Oct 9 at 10:23

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.