- Home /
Vector3 - transform.position
I have a script that gets a Vector3 located on the main camera and takes away a transform.position from it. What does this do? What is the point in it? What is this commonly used for. I don't know what this actually does. I'm guessing that it just takes my rigidbody's position away from another position but I don't get why.
Vector3 force = targetPoint - rigidbody.transform.position
Answer by highpockets · Mar 07, 2014 at 09:13 PM
The targetPoint is 1 Vector3 position and the rigidbody.transform.position is the position of the gameObject you are referencing with the rigidbody. The rigidbody position is subtracted from the targetPoint to get a force or difference Vector3. It's essentially finding the direction that the rigidbody has to travel in order to make it to the targetPoint. So, if force is put into a function like:
rigidbody.AddForce( force * speed );
You would effectively be moving the rigidbody toward the targetPoint. I put speed in there because it is typical to have a float controlling the speed at which the rigidbody moves at.
You have three floats in a Vector3 (xyz) and let's say the targetPoint is ( x=10, y=22, z=40 ) and the rigidbody.transform.position is ( x=5 , y=10, z=32 ). If you subtract the rigidbody.transform.position from the targetPoint, the product will be (x=5,y=12,z=8). So when you AddForce with the product, in your case, 'force', over multiple frames, recalculating targetPoint - rigidbody.transform.position and therefore changing the amount of force given in each direction (xyz) as the rigidbody gets closer to the targetPoint. The force will become less and less as you get closer to the targetPoint.
Does that make sense?
Oh yeah, thanks. I had AddForce in the script so yeah I understand it!