Quaternion.LookRotation()

I am evaluating Unity3D, having an issue with Quaternion.LookRotation(). Quick background: on the community site there’s a 2D Shooter project that is very enlightening in a number of ways. Our project is also 2D, though it has a “side view” (ie, looking along Z) rather than top down like the 2D shooter.

I’ve switched things around so that the camera can look along Z, but this is where the LookRotation() issue comes in. Basically, I can only get the expected results from LookRotation() when the “upwards” vector is passed as Vector3.up, can’t seem to make this work by passing Vector3.forward to make the up vector be along the Z. What does work is if I transpose the y and z components of the transform positions, do the LookRotation() around Vector3.up, then transpose the resulting Y rotation back around the original Z axis.

Quite prepared to believe I am doing something wrong.

Code:

var newRotation = Quaternion.LookRotation(other.transform.position - me.position, Vector3.forward);

A possible wrinkle is that I am using Blender meshes, which have a 270 degree rotation around X cooked into them (according to the inspector).

If anyone can spot my obvious error, or if this jangles any chords with anyone, I’d be very pleased for the input.

Steve

Welcome to the forums! Nothing looks wrong in your code off the start and so:

  1. LookRotation will create a rotation such that the transform’s local z-axis points along the world-relative “forward” direction specified. It will then attempt to align the transform’s local y-axis with the world-relative “up” direction specified (or in the same plane if not directly aligned). Does that match your expectations? Does that match your results?

  2. Have you tried using anything like Debug.DrawLine() inside of Unity to help debug things here? You can use that to draw lines inside the editor and literally see if the vectors you’re specifying do or don’t match the directions you’re attempting to use. This would help sort out whether this is a Blender rotation issue.

Sorry I don’t have anything more concrete, hopefully the above will help get you started towards a solution.

Good call on the Debug.DrawLine(), that definitely helped me to see what was happening and I have now figured out the issue. Basically, nothing in the LookRotation() method prevents a rotation around the Y axis, which was causing the 2D objects to go side-on to the camera.

Thanks for the tip!

Steve