Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Stopping RigidBody on a dime

Discussion in 'Scripting' started by daitoshokan, Aug 22, 2014.

  1. daitoshokan

    daitoshokan

    Joined:
    Apr 18, 2014
    Posts:
    6
    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:

    Code (csharp):
    1.  
    2. float moveHorizontal = Input.GetAxis ("Horizontal");
    3.         float moveVertical = Input.GetAxis ("Vertical");
    4.  
    5.         float horizontalspeed = moveHorizontal * maxSpeed;
    6.         float verticalspeed = moveVertical * maxSpeed;
    7.  
    8.         //print (moving);
    9.  
    10.         if(moveHorizontal!=0 && moveVertical !=0)
    11.         {
    12.             horizontalspeed = horizontalspeed/Mathf.Sqrt(2);
    13.             verticalspeed = verticalspeed/Mathf.Sqrt(2);  
    14.         }
    15.  
    16.         if (Input.GetKeyUp ("up") || Input.GetKeyUp ("down")) {
    17.                         print ("keyup");
    18.             rigidbody2D.velocity = new Vector2 (horizontalspeed, 0f);
    19.  
    20.         }
    21.             else
    22.         rigidbody2D.velocity = new Vector2 (horizontalspeed, verticalspeed);
    23.  
    24.  
    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!
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
  3. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    I find the best thing to do is detect when there is no input and do this

    Code (csharp):
    1.  
    2. if( noInput )
    3. {
    4.     rigidbody.AddForce( -rigidbody.velocity, ForceMode.VelocityChange );
    5. }
    6.  
    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.
     
    Last edited: Jul 12, 2016
    Galaxy_Surfer and jister like this.
  4. ScriptKid

    ScriptKid

    Joined:
    Aug 8, 2014
    Posts:
    23
    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
     
  5. daitoshokan

    daitoshokan

    Joined:
    Apr 18, 2014
    Posts:
    6
    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!
     
  6. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    this works well for 3D, except it's AddForce not AddVelocity... ;-)
     
    Antony-Blackett likes this.
  7. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102