- Home /
Rigidbody.Addforce against position
How can i use Rigidbody.Addforce, against a position? For example when i press "w" the object with rigidbody.addforce will be pushed against a choosen object.
Answer by Josh707 · Jun 06, 2014 at 08:45 PM
You would subract one object's position from another. To get a vector that points from object A to object B you would subract A's position from B's.
//Points to object B
Vector3 direction = objectB.position - objectA.position;
This will also be the magnitude/distance between the two, so you may want to normalize it before using it to add a force (Keeps the same direction but its magnitude is 1). The further they are, the larger the magnitude will be.
direction = (b.position - a.position).normalized;
AddForce(direction * someMultiplier);
I would love to try it out, but i says the first line Vector3 direction = objectB.position - objectA.position;
needs a ";".
What language are you using? If it's C# there may be an error on another line, JS would be written differently:
var direction : Vector3 = objectB.position - objectA.position;
Your answer
![](https://koobas.hobune.stream/wayback/20220613150943im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Pick and move physics object 1 Answer
Rigidbody.Addforce against position 0 Answers
Getting final position after rigidbody.addForce 5 Answers
Destroy object on collision or check position 2 Answers
Rotation problem with spawning bullet 2 Answers