I’ve been working on character movement for my game using Unity, and I’m running into a bit of an issue that I can’t seem to solve. I’m using `AddForce` for movement, which works great for getting my character to move around, but I’ve got this annoying problem: when I release the movement keys (like WASD), the character just keeps sliding instead of stopping instantly.
Here’s a snippet of my `FixedUpdate` method for context:
“`csharp
private void FixedUpdate()
{
moveHorizontal = Input.GetAxis(“Horizontal”);
moveVertical = Input.GetAxis(“Vertical”);
_direction = new Vector3(moveHorizontal, 0, moveVertical);
_rigidbody.AddForce(_direction * _speed);
}
“`
As you can see, I’m just adding force based on the input direction and speed. While this gives a nice acceleration and feel to the movement when pressing the keys, the sliding behavior is frustrating. I want the character to stop almost immediately when the keys are released, but right now, it feels like they have an ice skating problem!
I’ve thought about changing some Rigidbody properties or adjusting gravity, but I really don’t want to mess with those settings because I have other objects in my scene that rely on these properties. I’m worried it might create more problems than it solves, and I really want to maintain the overall physics consistency in my game.
So, I’m wondering if there’s a way to achieve that instant stop without changing the Rigidbody or gravity settings. Should I apply a counter force in the opposite direction when keys are released? Or maybe something else? I’m open to any suggestions that could help me fix this issue while keeping the movement feel intact. It’s driving me a bit mad! Any tips or ideas would be hugely appreciated!
The sliding issue you’re experiencing arises because using
AddForce
continuously accumulates momentum on your Rigidbody, which naturally results in a drift effect when input stops. Instead of altering physics settings globally, a straightforward workaround is to manually control the velocity by actively resetting or dampening it when no input is detected. For example, you can add a velocity check in yourFixedUpdate
method—instead of continuously applying force, directly set the Rigidbody’s velocity toward zero when input axes approach zero:By doing this, your Rigidbody gains smooth acceleration when keys are pressed but rapidly reduces horizontal velocity when no movement input is given, effectively fixing the “ice-skating” problem without affecting physics globally in your project.
Character Stopping Issue in Unity Using AddForce
It sounds like you’re dealing with a classic issue when using physics-based movement in Unity! When using
AddForce
, your character can indeed feel like it’s sliding right after you let go of the movement keys. To get around the ice-skating feeling, you might want to consider applying a damping effect or using a counter force.Here’s one way to solve the issue:
You can modify your movement logic to add a small counter force when the player stops pressing the movement keys. This way, you can slow the character down more quickly without changing Rigidbody properties. Here’s an example of how you can adjust your code:
This code sets the velocity to zero in the horizontal plane when there is no input, allowing your character to stop instantly. Don’t worry, you keep the vertical velocity (like gravity) intact, so it won’t mess up your jumping or falling mechanics.
Alternative Solution – Damping:
Another option is to use
Rigidbody.drag
. You can set a drag value to gradually reduce the speed of your character when no keys are pressed. This is an easy way to simulate friction without manually managing forces, but it won’t be as instant as the counter force option.Try out these suggestions, and see what feels best for your game. Happy coding!