Home > database >  How to set a limit for ridgidbodies speed
How to set a limit for ridgidbodies speed

Time:06-24

I have been working on a game with cars but they go to infinite speeds, and that's not nice.

I have no idea how to do it I tried rb.speed and stuff but I don't know how.

And if its not hard can you also explain how to access and change stuff in post processing (global volume object) URP (universal render pipeline) not normal unity.

CodePudding user response:

After saving rigidbody and defining a variable for speed limit, just limit it with Vector3.ClampMagnitude.

public float maxVelocity;

void FixedUpdate()
{
    ///... Add force

    rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxVelocity);
}

CodePudding user response:

Using the AddForce method can limit the maximum speed at which objects can move, although not very precisely. Methods as below:

public class PlayerMove : MonoBehaviour
{
    private float speed;
    void Update()
   {
        speed = Vector3.Magnitude(this.GetComponent<Rigidbody>().velocity);
        float force; //The size of the final force
        float maxPower = 20f;//Power
        if (Input.GetKey(KeyCode.W))
       {
           if(speed == 0) force = maxPower;//Prevent division by 0
           else force = maxPower / speed; // power formula force = power / speed
           this.GetComponent<Rigidbody>().AddForce(force * Vector3.forward);
        }
     }
 }
  • Related