Yes, yet another one of THESE questions. I know this has been asked numerous times before, but after scouring the board for days I can’t find a solution.
My game is a top-down space shooter, I’m working in the 2D mode in unity (on the XY plane).
Basically when the player comes within a certain range of an enemy I’d like the enemy to turn and face the player. I’m using a circle collider w/trigger to do the detection and capture the target transform (the player’s transform). That part works.
The issue (as always) is that the enemy will not turn around the Z-axis only when it turns. It rotates on all axis and usually ends up with the underside of the enemy sprite “facing” the player. When looking at it in 2D mode you end up looking at the edge of the sprite and it disappears.
Here is the code i’m using:
targetTransform = the player’s transform
transform = the enemy’s transform
` Vector3 vectorToTarget = targetTransform.position - transform.position; transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(vectorToTarget,Vector3.forward), 1 * Time.deltaTime); `
Here is the log output in a typical run.
TARGETPOS:(1.1, 1.4, 0.0)
SELFPOS:(5.0, 5.0, 0.0)
VECTOR:(-3.9, -3.6, 0.0)
All those look correct to me. I have a feeling the issue lies in how the rotation is being done.
Some solutions I have seen involve modifying the x or y components of the Quaternion that returns from Quaternion.LookRotation. While that does sort of work, I’ve also read numerous things that say to never modify the components on a Quaternion in that way.
example: http://forum.unity3d.com/threads/36377-transform-LookAt-or-Quaternion-LookRotation-on-1-axis-only
I am sort of lost here. Should I not be using Quaternions to do rotation in 2D? I’m also wondering if this could be caused by the game object being rotated. I’m thinking that possibly LookRotation is not sure what the front of the ship is. In the documentation for LookRotation there is this section:
Returns the computed quaternion. If used to orient a Transform, the Z axis will be aligned with forward and the Y axis with upwards if these vectors are orthogonal.I’m not quite sure what they are saying here. Does this mean that LookRotation always assumes that Z is forward? How would that ever work in 2D mode?
Any suggestions/help would be appreciated.
EDIT:
I can use :
transform.LookAt(transform.position + new Vector3(0,0,1), vectorToTarget);
to get it to work, but the rotation is immediate instead of a nice fluid turn over time that Slerp will provide.