Limit a Gameobject speed using ClampMagnitude
Hello,
I'm trying to make a simply asteroid game. I've run into a bug that I wasn't able to explain. Basically, sometimes the player got stuck at max speed with no chance to slow down. I found a dirty way to solve it: subtract by 0.001, but I would like to get to the bottom of this.
 void handleForwardMovement()
     {
         // Limit speed
         if (rb.velocity.magnitude > playerInfo.maxForwardSpeed)
         {
             // BUG: Sometimes you got stuck at max speed with no chance to slow down. This -0.001f solves this problem
             rb.velocity = Vector3.ClampMagnitude(rb.velocity, playerInfo.maxForwardSpeed-0.001f);
         }
         else
         {
 
             // Accelerate only forward
             float forwardAcceleration = Input.GetAxis("Vertical");
             if (forwardAcceleration > 0)
             {
                 rb.AddForce(forwardAcceleration * transform.up * playerInfo.forwardSpeed);
             }
 
         }
     }
 
              
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
Change the "jumping speed" in the FirstPersonController by using another script 0 Answers
Clamp a Vector3 position to an arbitrary line segment 1 Answer
How would i clamp X and Z without clamping Y? 1 Answer
ClampMagnitude while ignoring a direction 0 Answers
Jump OnCollision() Script not working 0 Answers