Stopping RigidBody on a dime

Hello,
I’m making a bullet hell inspired game which require very precise control of character movement. I do want use a RigidBody over direct manipulation of transform.position because there will be some physics based collisions in the game. However, when the player stops pressing the arrow keys, I want the character to come to a complete stop instantly. Currently, the character comes to a stop more gradually, as if they are decelerating. I have tried doing things like increasing the drag, but that seems to just generally slow the character down. Here is the relevant code from my fixedUpdate:

float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        float horizontalspeed = moveHorizontal * maxSpeed;
        float verticalspeed = moveVertical * maxSpeed;

        //print (moving);

        if(moveHorizontal!=0 && moveVertical !=0)
        {
            horizontalspeed = horizontalspeed/Mathf.Sqrt(2);
            verticalspeed = verticalspeed/Mathf.Sqrt(2);   
        }

        if (Input.GetKeyUp ("up") || Input.GetKeyUp ("down")) {
                        print ("keyup");
            rigidbody2D.velocity = new Vector2 (horizontalspeed, 0f);

        }
            else
        rigidbody2D.velocity = new Vector2 (horizontalspeed, verticalspeed);

You can see that right now I’m testing just the vertical axis for a fix. I pass the vertical component of the Vector2 “0” when the key comes up. However despite this, it’s velocity still does not instantly drop to 0, instead it falls off. I don’t understand why at all. Any insight on how to fix this would be greatly appreciated. Thanks!

There is Input.GetAxisRaw(). its without smoothing.
http://docs.unity3d.com/ScriptReference/Input.GetAxisRaw.html

1 Like

I find the best thing to do is detect when there is no input and do this

if( noInput )
{
    rigidbody.AddForce( -rigidbody.velocity, ForceMode.VelocityChange );
}

I find that if you do things like

rigidbody.velocity = Vector3.zero;

then collision won’t always react as expected.

Edit:
I also just noticed you’re using the 2D system. So ignore everything I just said. I haven’t tried this with the 2D system.

2 Likes

i use often raw axis + smoothdamp and then smooth the current velocity to 0 if i have no input in that velocity direction.
if u set there a variable named damptime or somthing and use it as factor for the time argument then u can set it to like 0 or -1 an your chacater will stop instantly or u make just a bit smooth to avoid stucking movement

Ah, so the problem game from how I was doing input then? My current control method is keyboard so I assumed it would naturally be discreet/without smoothing. I’ll test this out and let you know if it worked. Thanks!

Edit: Works perfectly now. I was even able to remove the “GetKeyUp” part now that smoothing is completely removed. Thank you so much for your reply!

this works well for 3D, except it’s AddForce not AddVelocity… :wink:

1 Like

THANK YOU!

1 Like