2D tranform.lookAt Problem

God knows y i always stuck here…2D game

I have a sprite , i want it look at on another object and move towards it. But when i use

spaceshipTransform.LookAt (_earth);

This rotates the sprite (since LookAt - Rotates the transform so the forward vector points at /target/'s current position.)

So how do i make the sprite just rotate by z axis (so that it looks fine) ?

Construct your sprite so that the front of the sprite is facing right when the rotation of the sprite is (0,0,0). Then you can do:

 var dir =_earth.postion - transform.position;
 var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
 transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

This assumes that _earth is a transform. If it is a Vector3, just use ‘_earth’ rather than ‘_earth.position’.