- Home /
Make Rigidbody Character retain momentum
Hi all,
Currently my character moves fine, but whenever it touches the ground, it quickly clamps its velocity magnitude to it's max speed value. What I want is (while holding forward) for the character to keep it's velocity.magnitude, and lose speed very slowly (preferably being able to tweak this rate), until we're back at the max speed value. Here is all (I think) of the relevant code:
xInput = Input.GetAxis("Horizontal");
zInput = Input.GetAxis("Vertical");
Vector3 velocity = rb.velocity;
Vector3 hVelocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
Vector3 addForce = new Vector3(xInput, 0, zInput);
addForce = transform.TransformDirection(addForce);
if (grounded && zInput == 1)
{
//??? I want the character to retain most of its momentum while this is true
}
addForce *= maxSpeed;
addForce = (addForce - velocity);
if (grounded)
{
addForce.x *= 10;
addForce.z *= 10;
//makes acceleration quicker
}
rb.AddForce(addForce);
//rb.AddForce(velocity.normalized * (-velocity.magnitude * drag));
//(right now drag is set to 0)
rb.AddForce(new Vector3(0, -gravity * rb.mass, 0));
Thank you!!
Comment