- Home /
Decrease character controller velocity by percent each frame
I've been trying to wrap my head around how you would go about reducing the player's speed by a certain percentage every second. For example, if I wanted to slow the player from an arbitrary speed of 100 over the course of 10 seconds, I would need something that reduces their speed by 10% every second. In my case, I need framerate independent ground friction using a character controller.
private void applyFriction()
{
Vector3 tempVelocity = playerVelocity;
tempVelocity.y = 0;
tempVelocity = tempVelocity - (Time.fixedDeltatime * tempVelocity);
//assuming a timestep of 0.02 seconds, then roughly tempVelocity should decrease by 1/50th of itself every 0.02 seconds at 50 physics calculations per second.
//since velocity changes on a frame by frame basis, and since fixedDeltaTime could fluctuate, then how do we account for this change?
velocity.x = tempVelocity.x; velocity.z = tempVelocity.z;
}
In practice, given a Vector3 (representing velocity), how can you reduce the Vector3's magnitude by a certain percentage each second?
Or as a more general question, what is the best way to represent friction using a character controller? What formula would I apply to tempVelocity in order to slow the character down by a certain percentage every second?
I have tried a couple of things involving addition already. Using a rigidbody is out of the question, though I do think seeing how a rigidbody handles this sort of thing would be helpful.
Your answer
![](https://koobas.hobune.stream/wayback/20220613015618im_/https://answers.unity.com/themes/thub/images/avi.jpg)