0
\$\begingroup\$

So I have an object that is bouncing around the screen randomly, physics still works on it so it will speed up or slow down depending on what it hit and when. To try to normalize the speed I want to apply force or drag when ever it goes above or below the thresh hold. My question is, do I need to make note of the direction the object is moving to add force? Or can I just add force to the way it is currently traveling?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ What have you tried so far? I expect this will become clearer for you as soon as you type rigidbody.AddForce( and start browsing the auto-complete suggestions. Is what you've written so far not behaving the way you expect? \$\endgroup\$
    – DMGregory
    Oct 28, 2019 at 13:42

1 Answer 1

1
\$\begingroup\$

Forces are directional, so if you want to decelerate an object by using rigidbody.AddForce, you need to do that with a force vector which goes in the opposite direction of the current velocity. The easiest way to obtain such a vector is by multiplying rigidbody.velocity with a negative value. For example, if you want to add a deceleration force which reduces the speed by 10% per second, you would do:

rigidbody.AddForce(rigidbody.velocity * -0.1);

rigidbody.drag, on the other hand, is a scalar value. When you increase the drag value, you increase it in any direction.

If you want to prevent an object from comming to rest, then you could always add a force which accelerates it into its current movement direction. I would recommend you to do that by taking the normalized property of the velocity. This gives you a vector which points in the same direction as the current velocity, but with a length of exactly 1 unit. If you always want to permanently accelerate the rigidbody with a constant force in its current movement direction:

 rigidbody.AddForce(rigidbody.velocity.normalized * 0.1)

But this does need a minimum velocity to work with. If the object is perfectly at rest (rigidbody.velocity == Vector3.zero) then it will stay there until something gives it a little nudge.

\$\endgroup\$
1
  • \$\begingroup\$ This is exactly what I was looking for, thank you. Yeah I have noticed it will come to rest some times, other times it will fly off at a crazy speed so I want to try to normalize that speed. So if I can use that normalized function that should help a ton, thank you very much! \$\endgroup\$
    – Ryan Fiser
    Oct 28, 2019 at 14:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .