- Home /
Understanding transform.LookAt()
Hi all! I was trying to make the face of my character to look at some target, I used lookat function but it was always rotating the head in some other direction and head was never facing the target, but when I tried this on cube the cube rotates exactly the way it should. Then I made the head child of an empty gameObject and pass the empty parent gameObject as head and everything worked fine. So my question is what was the problem before making this parent child realtionship? Thanks in advance.
Answer by Bieere · Oct 08, 2015 at 05:46 PM
So essentially the LookAt() function takes a transforms forward, and points it towards another transforms current World Position.
So if your transform you're trying to rotate to lookat something has its front not on the forward Vector, it might give you a weird rotation similar to what you're experiencing.
So the child parent setup you mentioned is likely the easiest way to solve your issue, now if you're up for doing some Vector maths, you could correct your rotation issue.
Sorry for my late answer, so I understand that the forward vector of both(target and the head) should be same(head's forward should be in the direction of target's forward, correct me if I am wrong), you mentioned vector math what kind of vector math do I need I mean what methods are there that can help me in this thing. Basically I was trying to do I$$anonymous$$ on arms to hold the gun I had done very good so far but still having some issues. Hands are not exactly holding the gun the rotate very well like a real human arm but they don't hold the gun properly like if gun is at (5, 1, 5) they will try to approch (2, 1, 5). So hands will try to reach left side or right side of the gun but not the gun.
Uhm, no. The forward vector of your target is completely irelevant.
Just draw an imaginary line between your player position and your target position. LookAt will rotate your player so the players blue arrow (the forward vector) points along this imaginary line, so it points at your target. If the transform of your head doesn't have the blue axis sticking out of the characters nose (but ins$$anonymous$$d at the top / bottom / the sides / back), LookAt will of course rotate the wrong side towards the target.
If you want to use LookAt with an object that is incorrectly rotated, you can insert another empty child object in between to fix the rotation:
Add an empty GameObject to the same parent that your head is attached to. $$anonymous$$ake sure it's positioned at the same point where your head is located.
Rotate the empty GO, so that the blue axis points out of the characters face and the green axis points upwards (red should point to the characters right side).
Once it's rotated correctly, just move the Head object inside the empty GameObject.
Now make sure when you want to rotate the head to rotate the empty GO ins$$anonymous$$d of the actual head.
Thank you so much I was thinking the whole time that target and head both should have there forward vectors in the same direction and your answer cleared my concepts pretty much. I think I should post another question to clarify I$$anonymous$$ concepts and some vector maths.
Your answer

Follow this Question
Related Questions
Optimization of the game and Viking Village asset 2 Answers
How do i code a Mesh collider ? 1 Answer
Two instances of Unity3D 5 0 Answers
Button Enabling/Disabling using Collision Triggers? 1 Answer