- Home /
 
How to add a velocity limit to an object with rigibody addforce?
Hello, community! Currently, I´m facing a challenge. I am working on a mobile game in which I move the player with in-game joystick system. The problem is that the player is too fast if the drag of the joystick is long enough. I would like to somehow limit my player´s velocity. Any ideas? Thank you so much. code :
  void FixedUpdate()
     {
     Vector2 moveDirection = UltimateJoystick.GetPosition("Movement");
 
         if (moveDirection != Vector2.zero)
         {
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0.0f, 0.0f, Mathf.Atan2(moveDirection.y, moveDirection.x) * Mathf.Rad2Deg), 0.1f);
         }
       
     Vector2 movement = new Vector2(moveDirection.x, moveDirection.y);
     rb2d.AddForce(movement * speed * movementspeed *2);
     }
 
              
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by Ferrarie_SoKingwah · Aug 14, 2018 at 03:14 PM
/the velocity will be always between yourMinLimt to yourMaxLimit /
 rb2d.velocity = Mathf.Clamp( rb2d.velocity , yourMinLimt, yourMaxLimit );
 
              thanks for a reply. Anybody that would like to know how I decided to implement it to my code:
 rigibody2D.velocity = Vector2.Clamp$$anonymous$$agnitude(rigibody2D.velocity, 250f);
                 this code will get error in editor. i don;t realise that rb2d.velocity is vector2 type.Sorry for that
      rb2d.velocity = $$anonymous$$athf.Clamp( rb2d.velocity , your$$anonymous$$inLimt, your$$anonymous$$axLimit );//error
                 Your answer