Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

askthedev.com Logo askthedev.com Logo
Sign InSign Up

askthedev.com

Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Ubuntu
  • Python
  • JavaScript
  • Linux
  • Git
  • Windows
  • HTML
  • SQL
  • AWS
  • Docker
  • Kubernetes
Home/ Questions/Q 39617
In Process

askthedev.com Latest Questions

Asked: June 5, 20252025-06-05T10:14:20+05:30 2025-06-05T10:14:20+05:30

How can I make my character stop instantly using AddForce without altering Rigidbody or gravity properties?

anonymous user

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!

  • 0
  • 0
  • 2 2 Answers
  • 0 Followers
  • 0
Share
  • Facebook

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Continue with Google
    or use

    Forgot Password?

    Need An Account, Sign Up Here
    Continue with Google

    2 Answers

    • Voted
    • Oldest
    • Recent
    1. anonymous user
      2025-06-05T10:14:21+05:30Added an answer on June 5, 2025 at 10:14 am

      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:

      private void FixedUpdate()
      {
          moveHorizontal = Input.GetAxis("Horizontal");
          moveVertical = Input.GetAxis("Vertical");
      
          _direction = new Vector3(moveHorizontal, 0, moveVertical);
          
          // If no input, apply a counter force to stop the character
          if (_direction.magnitude == 0)
          {
              _rigidbody.velocity = new Vector3(0, _rigidbody.velocity.y, 0);
          }
          else
          {
              _rigidbody.AddForce(_direction * _speed);
          }
      }

      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.

      private void Start()
      {
          _rigidbody.drag = 5f; // Adjust this value to your liking
      }

      Try out these suggestions, and see what feels best for your game. Happy coding!

        • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    2. anonymous user
      2025-06-05T10:14:22+05:30Added an answer on June 5, 2025 at 10:14 am

      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 your FixedUpdate method—instead of continuously applying force, directly set the Rigidbody’s velocity toward zero when input axes approach zero:

      
      private void FixedUpdate()
      {
          moveHorizontal = Input.GetAxis("Horizontal");
          moveVertical = Input.GetAxis("Vertical");
          _direction = new Vector3(moveHorizontal, 0, moveVertical);
      
          if (_direction.magnitude > 0.1f)
          {
              _rigidbody.AddForce(_direction.normalized * _speed);
          }
          else
          {
              Vector3 velocity = _rigidbody.velocity;
              velocity.x = Mathf.Lerp(velocity.x, 0, 0.5f);
              velocity.z = Mathf.Lerp(velocity.z, 0, 0.5f);
              _rigidbody.velocity = velocity;
          }
      }
      

      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.

        • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    Sidebar

    Recent Answers

    1. anonymous user on How do games using Havok manage rollback netcode without corrupting internal state during save/load operations?
    2. anonymous user on How do games using Havok manage rollback netcode without corrupting internal state during save/load operations?
    3. anonymous user on How can I efficiently determine line of sight between points in various 3D grid geometries without surface intersection?
    4. anonymous user on How can I efficiently determine line of sight between points in various 3D grid geometries without surface intersection?
    5. anonymous user on How can I update the server about my hotbar changes in a FabricMC mod?
    • Home
    • Learn Something
    • Ask a Question
    • Answer Unanswered Questions
    • Privacy Policy
    • Terms & Conditions

    © askthedev ❤️ All Rights Reserved

    Explore

    • Ubuntu
    • Python
    • JavaScript
    • Linux
    • Git
    • Windows
    • HTML
    • SQL
    • AWS
    • Docker
    • Kubernetes

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.