Hello. When using transform.LookAt, is there a way to restrict the object's rotation so that no matter where the "target object" goes, the object that is doing the looking does not rotate at all around the y axis?
Hi,
Another way is to change the target and modify the position to bring it on the plane perpendicular to the axis you want it to rotate around.
// Look only at the target by rotating along the y axis
Vector3 lookPos = LookAtTarget.position -transform.position;
lookPos.y = transform.position.y;
transform.rotation = Quaternion.LookRotation(lookPos);
That should do that trick,
Bye,
Jean
You cannot restrict the LookAt function. However you can override it just after you have called it with changing the eulerAngles (rotation in degrees).
C# code
float preRot = transform.rotation.eulerAngles.y;
transform.LookAt (somethingFunny);
Vector3 rot = transform.rotation.eulerAngles;
rot.y = preRot;
transform.rotation = Quaternion.Euler (rot);
It's a bit messy in C# because the eulerAngles is a property, not a field, which means I can't assign eulerAngles.y, I have to assign the whole Vector3 to it directly.
Js code (will probably compile, not used to writing Js)
var preRot : float = transform.rotation.eulerAngles.y;
transform.LookAt (somethingFunny);
transform.rotation.eulerAngles.y = preRot;