- Home /
Question by
Ultimate360 · Mar 16, 2017 at 09:12 AM ·
triggerquaterniontargetingrotatetowardsquaternion.lookrotation
Object rotate to target in 2D? Not working properly.
I search the forum and found this: (I'm confused/curious)
http://answers.unity3d.com/questions/650460/rotating-a-2d-sprite-to-face-a-target-on-a-single.html
So I made my code just like this:
Vector3 vectorToTarget = transform.position - targetObject.transform.position;
float angle = Mathf.Atan2 (vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
Quaternion q = Quaternion.AngleAxis (angle, Vector3.forward);
transform.rotation = Quaternion.Slerp (transform.rotation, q, Random.Range (0.1f, 0.2f));
The result: my object (enemy) facing 90 degrees (offset) right to the target (player). I want to know what's going on here? Why is this happening?
Instead I made: q * Quaternion.Euler(new Vector3(0f, 0f , 90f))
in order just to work properly?
Vector3 vectorToTarget = transform.position - targetObject.transform.position;
float angle = Mathf.Atan2 (vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
Quaternion q = Quaternion.AngleAxis (angle, Vector3.forward);
transform.rotation = Quaternion.Slerp (transform.rotation, q * Quaternion.Euler(new Vector3(0f, 0f , 90f)), Random.Range (0.1f, 0.2f));
rotatetotarget-offset.png
(117.1 kB)
Comment