- Home /
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) ?
I don't understand your problem
This rotates the sprite
isn't this what you need?
Answer by robertbu · Nov 28, 2014 at 08:20 AM
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'.
Your answer
Follow this Question
Related Questions
2D look at upside down. 2 Answers
Problem with Quaternion.LookRotation() 1 Answer
How to define the "front" of an object? 0 Answers
Change the face of lookat 2d 0 Answers
Looking at target in 2D 1 Answer