- Home /
Preventing "LookAt" from flipping?
Essentially I'm creating a 2D game with 3D graphics in Unity iPhone. To minimise the poly count I have cut the backs off many of the objects because you will only see them from the front. My problem is the "LookAt" code. I'm saying LookAt(Vector2) for a 3D arrow to look at something on the X and Y axis, the problem is when the object passes it on the x axis the arrow flips round to show the back of itself. So for example when the target is to the right of the Arrow its all fine but when it's to the left of the arrow it shows the back of the arrow. Does anyone have any idea how I can just limit the arrow to pointing towards the object and not rotating around itself?
Thanks in advance
have you tried using a configurable joint assigned to the 3d arrow? You can use it to lock rotation to certain axes pretty easily.
Answer by Atnas1010 · Oct 29, 2010 at 12:34 PM
If I have understood the problem correctly, I think you could use this code instead of "LookAt"
var relativeUp = target.TransformDirection (Vector3.forward);
var relativePos = target.position - transform.position;
transform.rotation = Quaternion.LookRotation(relativePos,relativeUp);
You might have to change the Vector3.forward to something else, and/or add an amount of rotation, based on how your current setup is.
Does this solve the problem?
This solved my problem, though now I have a bit of a "Roll" problem where my character/camera is slowly rolling after some combination of pitch and yaw rotations.