- Home /
Slow down Rigidbody's speed to a stop
I'm trying to make my player slide a bit when the input key is let go and I want it to decrease over a few frames. How would I do this? Here's my movement code:
float p = Input.GetAxisRaw("Horizontal");
if( p > 0){
myRigidbody.velocity = new Vector3 (activeMoveSpeed, myRigidbody.velocity.y, 0f);
transform.localScale = new Vector3 (currentValue, currentValue, 1f);
} else if (Input.GetAxisRaw ("Horizontal") < 0f) {
myRigidbody.velocity = new Vector3 (-activeMoveSpeed, myRigidbody.velocity.y, 0f);
transform.localScale = new Vector3 (-currentValue, currentValue, 1f);
} else if(p <= 0)
{
//Slide code goes here
}
why don't you use a Physics$$anonymous$$aterial which applies friction to it on it's own? Or use the rigidbodies drag value.
I already have a different material attached to my player's rigidbody and the drag doesn't let my player jump properly
Answer by Kudorado · Dec 03, 2018 at 01:42 AM
you can try this one :
myRigidbody.velocity -= myRigidbody.velocity / 10f;
Another way of making a similar change:
myRigidbody.AddForce(-myRigidbody.velocity, Force$$anonymous$$ode.Acceleration);
@$$anonymous$$udorado Ok that works but now if I jump while idle it reduces the jump speed. How would I disable it when my player is not on the ground. (I already have a bool checking if it's on the ground or not)
Answer by username123445 · Dec 01, 2018 at 07:47 PM
create friction in the code and don't apply it when you jump
Your answer
Follow this Question
Related Questions
how to keep speed canstant in flappy bird? 2 Answers
DragObject Damping 2 Answers
Dragging movement Speed 1 Answer
How to read the movment speed of sprite and flip basied upon it 1 Answer