0
votes

I am trying to rotate an object to face another in Unity 2D. However i am having some trouble wrapping my head around the way unity uses 2D transformations and most examples I can find are designed for 3D.

Using the code below I can make the object rotate to face the right direction, however it also rotates the object so that it is the Z axis that is pointing towards it. Ideally the object would rotate around the Z axis.

rigidbody2D.transform.LookAt(currentlyTargeted.transform.position);

Any guidance would be much appreciated.

2
Did you tried to set the worldUp parameter and check how it behaves? docs.unity3d.com/Documentation/ScriptReference/…OnoSendai
Thanks, I diddnt realize that existed. However I have tried every combination of Vector3's (up,forward,down,left,right) and I still seem to have the same problem. The Z axis points to the object. rigidbody2D.transform.LookAt(currentlyTargeted.transform.position, Vector3.up);user2779315
On similar issues, I always found up that I had my object set incorrectly, or forgot I've transformed it beforehand.OnoSendai

2 Answers

0
votes

I had a similar issue recently with LookAngle - please notice these functions take two arguments (the second one defaults to Vector3.up) - what did the trick for me is to call LookAngle like this:

LookAngle(Vector3.forward, myCalculatedPositionsDifference)

Maybe it will also help in your case?

1
votes

Make sure that your object's direction in the mesh/sprite is rotated correctly, or place the object in a dummy object used to offset the rotation to the X/y axes