Now, I get that the below code looks at the target no matter where the are in a 3D space, but I don’t understand how it works. I need (want) to know the math, what happens when we negate a position from another? I’m super curious.
// Most of the time you can use:
// transform.LookAt instead
var target : Transform;
function Update () {
var relativePos = target.position - transform.position;
var rotation = Quaternion.LookRotation(relativePos);
transform.rotation = rotation;
}
transform.position is of type Vector3, which can represent a point in space.
A vector, however, can also describe a direction. In simple terms, say you have a directional Vector3 with the values (0, 1, 0). This means that you are describing a direction starting from (0, 0, 0) and pointing towards (0, 1, 0). By negating two Vector3, you get a direction pointing from the subtracted value towards the other point.
If transform.position is (1, 0, 1) and target.position is (2, 0, 3) you’ll get the result (1, 0, 2) if you subtract them. Likewise, if you move both points together in way that transform.position is at (0, 0, 0), target will move to position (1, 0, 2). That’s the reasoning behind it.
On to some extra related information:
Using directional vectors is very useful. It often makes sense to normalize the directional vector. This means that the vector keeps the exact same direction, only its values are adjusted so that it has a length of 1.
check out this code:
var direction = (target.position - transform.position).normalized;
transform.position = transform.position + direction * 5;
This will move the current object 5 units towards the target. Had I not normalized the direction, you wouldn’t have moved 5 units towards the target, but 5 times the current distance.
Hey, thanks for this. Sorry I should have been more clearer. I know a little Vectors, and their math. What I don’t understand is how we get a the targets direction/position from taking our position away from it.
For example,
playerposition is at (4, 0, 5)
targetposition is at (6,0, 9)
using :
var direction = (target.position - transform.position).normalized;
transform.position = transform.position + direction * 5;
direction would equal (2, 0, -4)
I assume this is a direction, not a vector. - This may be where I get confused, as if it was a vector, it makes no sense to move towards that vector position, as it is not targets position. However, adding this direction to the current position (am i right in thinking it’s also distance?) will give the target position.
Oh, wait I see… we’re getting the direction between two vectors, then adding that direction (or distance, i’m inclined to say distance) to our position with a multiple/addition/whatever. That makes sense in my head.
// Note - I really don’t know, it is the distance, its also the sum of subtraction, and I guess it could also be distance.
In my example in the first post, I still don’t quite understand…
var relativePos = target.position - transform.position;
var rotation = Quaternion.LookRotation(relativePos);
transform.rotation = rotation;
So, we’re getting our relativeposition, which using the above would again be (2, 0, -4).
We’re then saying that rotation = Quaternion.LookRotation(2, 0, -4);
I think this is where I’m confused… what is Quaternion.LookRotation doing?
is it looking at that vector, or simply adding/multiplying that direction to our current rotation?