- Home /
Moving On Two Axes and Reading a Stable Angle
Hi All!
This is my first time posting to this forum and I hope you can help me out. I am a complete newbie when it comes to Unity programming, but not programming in general.
What I'm Trying to Do: I am trying to make an instance of a spotlight follow a player object but only use two axes to do it (emulating a moving head type of light for a DIY WISYWIG visualizer). So, specifically, I have a moving light model containing a spotlight within a head which tilts on the X axis connected to another object which pans on the Z axis. My moving light object has within it a dummy light object which I use to look at the player object.
Specific Problem: Here is the crux of the issue: After looking at the player object using only the X and Z axes, I want to extract the rotation angle data so I know how many degrees pan and tilt resulted in the light focus.
Where I'm At: Using lookAt I have a light tracking an object but using XYZ. When I use slerp and lookRotation like so:
var targetPoint = new Vector3(targetObj.position.x, 0, targetObj.transform.position.z) - lightObj.position; var targetRotation = Quaternion.LookRotation(targetPoint); lightObj.rotation = Quaternion.Slerp(lightObj.rotation, targetRotation, 1);
the axes do not switch about during tracking, but the EulerAngles are unstable. I understand referencing the EulerAngles within unity isn't wise because there are many possible XYZ combinations for a given Quaternion. Hence this issue in general. I read about using Vector3.Angle to compare two vectors, and tried to do so as follows:
//lightObj is looking at the player while refLight is identical but static as a reference panAngle = Vector3.Angle(new Vector3(0,0,lightObj.rotation.z), new Vector3(0, 0, refLight.rotation.z)); tiltAngle = Vector3.Angle(new Vector3(lightObj.rotation.x,0,0), new Vector3(refLight.rotation.x,0,0));
After much experimentation I have run up against a knowledge wall and I know no one personally experienced with Unity programming.
Any help you could offer will be greatly appreciated!
Your answer
Follow this Question
Related Questions
LookAt on only 1 axis for 2D games? 1 Answer
LookAt script Issues 0 Answers
Look Rotation flipping object rotation after 180 degrees 1 Answer
lookAt euler angles 3 Answers