Implementing Friction?

I’m making a 3D platformer, and at the moment my character is quite floaty and slippery when moving around. I want to implement friction, so the movement will be much tighter.

At the moment, my plan is to, every frame, subtract from the character’s velocity. When the character stops moving, it should obviously stop at 0.

The problem I’m having is that I don’t know how to implement this when both positive and negative values exist. The top speed is 4, so obviously the character’s velocity on both x and z axis needs to be clamped at -4 to 4. But how would you then ensure that when there is no input that the velocity cannot pass 0?

Or am I thinking about this the wrong way? Is there a better way to implement friction? Really struggling with this one.

All help appreciated, thanks!

There are a lot of ways to do this, but I wouldnt recommend subtracting velocity. This is really only because it means for logic and can make some clutter. A method I would recommend for this is Vector3.Lerp. What this function does is take a vector and interpolate it into another vector over time (in your case Vector3.zero).

Alternatively if you don’t want to use that function, you could just multiply your velocity vector by a decimal, say 0.5, per frame. This will of course asymptote at 0 though and your velocity will never technically reach 0.

Both of these function don’t need any logic on the surface to compansate for negative directions, and will both approach 0 without exceeding.