- Home /
Adding a force in the direction of an object.
I am adding a force to object A towords object B by wrighting
rigidbody.AddRelativeForce(target.position force);*
Im using relitive forces because i whant it to moove in lical not global space.
But for this to work i must use
transform.LookAt(target);
But i dont whant to have to rorait the object i whant to rotat the direction of the force towards object B, how can i do this or if not what can i do to give the same effect.
Answer by homer_3 · Feb 24, 2012 at 10:31 PM
Couldn't you just get the vector going from your source to your target and use that with AddForce like
dir = target.transform.position - transform.position;
dir = dir.normalized;
rigidbody.AddForce(dir * force);
Answer by finndaly · Feb 24, 2012 at 10:34 PM
But will it update the direction if it is in the update function without rotaiting the object, and can you explain how it works.
dir = target.transform.position - transform.position; (is this a var in which holds a vector3 of the target?)
dir = dir.normalized; (What dose normalized do to dir.)
rigidbody.AddForce(dir * force); (and so im gesing that this adds the force to dir which is the set vector3 multiplyed by the force that i set.)
Subtracting the target's position from your position will get you the vector going from you to the target. Normalize is a well known mathematical operation. If you don't know what it means look it up. But you do it so you won't have to adjust your force value based on the magnitude of dir. AddForce's documentation is on Unity's website.
Your answer
Follow this Question
Related Questions
How to change gravity's direction on a single gameobject? 0 Answers
How to make a character look at the same direction it's moving? with c# 0 Answers
How to get object to face direction it's flying rather than looking at the target 2 Answers
Instantiating in a specific place with force 2 Answers
how to apply a force in direction of object is facing? 2 Answers