- Home /
LookAt in opposite direction?
I don't understand why 3dtext has backwards orientation, but I need to use transform lookat with the text mesh. I don't want to dump it in a another game object, I just need to look at the camera, without the letters being backwards! How do I reverse transform.LookAt?
Thanks alot!
The orientation isn't backwards, though. Having it look at the camera would mean the forward vector is facing towards the camera. This is only "backwards" because you've rotated it 180 degrees from the way it would normally be facing.
Answer by superventure · Jun 21, 2011 at 12:11 AM
Found an answer after more browsing (sorry)
transform.LookAt(2 * transform.position - stareat.position);
Serious question: why does multiplying by two work? I've tried this solution in a project and don't understand the math behind it.
Thanks!
Simple vector math. To get a direction vector from a point to another you do "target - start". In this case we calculate a direction vector from the object we want to look at (stareat.position) to our own position. So the resulting relative direction vector points towards us.
By adding our own position to that vector we let the direction vector start at our position. Since the direction points towards us it will be result in the position behind us.
In this image we are the red dot (p1) and our second object is the green dot (p2). So instead of using p2 as lookat target we use p3 which is exactly behind us (the red dot). You get p3 by using the blue direction vector from p2 to p1 and adding it to p1.
p1 + (p1-p2) == p1 + p1 - p2 == 2*p1 - p2
Another way to look at it is this:
$$anonymous$$aybe i should add that this solution is actually more complicated than $$anonymous$$e below. LookAt most likely uses LookRotation internally. It simply subtracts it's own position from the given point to get a direction vector towards the target. So if you pass
2 * transform.position - stareat.position
and LookAt subtracts transform.position
from that given vector it would be
2 * transform.position - stareat.position - transform.position
which is just
transform.position - stareat.position
Answer by Bunny83 · Jun 21, 2011 at 12:14 AM
That will do what you want. It rotates the transform "away" from the target.
var target : Transform;
function Update ()
{
transform.rotation = Quaternion.LookRotation(transform.position - target.position);
}
Thanks for this!
I was trying to figure out how to do it with Quaternion.LookRotation specifically. This works perfect!
I can't believe it was as simple as swapping the transform and the target.
Your answer
Follow this Question
Related Questions
make player move in camera direction 1 Answer
Smoothly Move camera while looking at object 0 Answers
LookAt only when camera position changes 2 Answers
An alternative to RotateAround for collisions 0 Answers
transform.lookat() limitation... 1 Answer