Daniele Pappalardo
Daniele Pappalardo

Reputation: 104

How can I set a constant speed with AddForce of the rigidbody?

I want to move an object with AddForce but I don't want its speed to increase dramatically, without a limit, but to move with a speed limit set by a float value.

Upvotes: 1

Views: 3732

Answers (2)

sommmen
sommmen

Reputation: 7660

You can use clamp to restrict your value(s);

https://docs.unity3d.com/ScriptReference/Mathf.Clamp.html

Upvotes: 0

Emil Terman
Emil Terman

Reputation: 526

You could probably use Vector3.ClampMagnitude:

rigidbody.AddForce(...);

float maxSpeed = 10;
rigidbody.velocity = Vector3.ClampMagnitude(rigidbody.velocity, maxSpeed);

And don't forget to play with the force inside FixedUpdate() not in Update()

Upvotes: 4

Related Questions