- Home /
lookAt euler angles
Hi,
my question is: Is there any way to get the eulerAngles that are required to look at another GameObject without performing the Lookat().
In other words, I want the angles as Vector that I can modify without performing a lookat.
It should look like this: Vector3 lookAtRotation = GetLookAtEulerAngles(gameobject2);
Kind regards
Answer by Fattie · Aug 22, 2012 at 04:48 PM
http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.LookRotation.html
if you want the euler angles...
http://docs.unity3d.com/Documentation/ScriptReference/Quaternion-eulerAngles.html
Hi since you're a new user could you please TICK any correct answers, cheers
This is pretty close to what I'm searching for, but ins$$anonymous$$d of an quaternion, I want the euler angles.
First link: Working with quaternions, don't know how to get out the correct eulerangles. Second link: You create a empty quaternion, to set angles. That's heading the wrong direction ^^
Sorry but still don't get it :(. Lets do an example:
-Step1:GameObject one (position=(0,0,0)) and two (position=(1,1,1)) -Step2:Calculation direction vector (1,1,1)-(0,0,0)=(1,1,1) -Step3:Get out of this direction vector the angles where the vector is pointing to WITHOUT using quaternions - that's what I can't get ahead.
Fattie's answer is correct. For your example where direction is (1,1,1):
// This gives the answer as a quaternion
Quaternion q = Quaternion.LookRotation(direction);
// to get the angles just access eulerAngles on the quaternion which LookRotation returned:
Vector3 angles = q.eulerAngles;
Answer by Cool Dave · Aug 22, 2012 at 05:15 PM
I don't know exactly how to do that, but here's some snippets I grabbed from an AI script of mine. I had had a very similar problem. If you figure it out, post it here.
xDistance = target.position.x - carCenter.position.x;
zDistance = target.position.z - carCenter.position.z;
var angle : float = Mathf.Atan2(xDistance, zDistance) * Mathf.Rad2Deg;
carAngle = carCenter.eulerAngles.y;
steering = (angle - carAngle);
Your example is in 2D-Space, but works fine I think. I did the same for 3D and works anyway. But there must be a shorter way or an default method to call.
Thx anyway!
Thank You very much for this, im searching this for hour. but after tried it. its following at the opposite angle of 180.. so i modified the code a bit like this
var xDistance = target.position.x - transform.position.x;
var yDistance = target.position.y - transform.position.y;
float angle = $$anonymous$$athf.Atan2(xDistance, yDistance) * $$anonymous$$athf.Rad2Deg;
var targetAngle = target.eulerAngles.z;
var result = (targetAngle - angle);
transform.eulerAngles = new Vector3(0f, 0f, result+180f);
Answer by marius.treu · Aug 22, 2012 at 07:02 PM
Extrension:
To solve this problem I usually calculate the distance vector and calculate each axis x,y,z with sin / cos. That's kind of a roundabout way. There must be an defined method!