- Home /
Replacement for LookAt in 2D
I've been looking for the last hour for a solution for the ongoing problem that LookAt is basically incompatible with 2D rotations, especially with sprites, and every suggested workaround hasn't worked for me.
I'm currently working on a 2D pool game for touchscreens. Basically the pool cue is clamped to where the user is touching and should ideally rotate to track around the cue ball. The cue is a basic sprite asset, as is the ball.
Answer by robertbu · Feb 08, 2014 at 06:28 AM
This code will work:
Vector3 dir = ball.position - transform.position;
float angle = Mathf.Atan2(dir.y,dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
Make sure that when the cue's rotation is (0,0,0), that the cue is pointing to the right. You probably also want the anchor for the sprite to be on the middle right of the sprite.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Hop from transform to transform in 2D game? 0 Answers
How to convert to 2D 1 Answer
2D PlatformerController: Cumulative drift? 1 Answer
2d Game Jump Issue 2 Answers