- Home /
The question is answered, right answer was accepted
LookAt 2D Equivalent ?
Is there a Transform.LookAt() Equivalent that will work for 2D ?
Without turning the gameobject on it's side that is...
Or alternatively does anyone have a workaround using Eulers or something ?
Thanks in advance :)
:
edit : Solution
Quaternion rotation = Quaternion.LookRotation
(Target.transform.position - transform.position, transform.TransformDirection(Vector3.up));
transform.rotation = new Quaternion(0, 0, rotation.z, rotation.w);
Seams to work for what i am trying to achieve but please if anyone comes up with a better solution let me know.
Thanks so much. You just solved a problem I've been having for days!
Just wanted to drop in and say this code solved my issues of having my projectile instantiate with the wrong rotation. This code works great. Thank you!
transform.LookAt(Vector3(target.position.x , target.position.y , fixedZ));
I tried using this code to make a gameobject to "follow" my cursor
But I get this problem where it believe that the position of my gameobject is (0,0) so I have to move my mouse down in the left corner to get the gameobject to rotate.
What am I doing wrong? (I have the Camera as a child under my "player" gameobject, does that have anything to do with this?)
Please help <3
Answer by daivd.ramz · Feb 11, 2014 at 04:40 PM
hi guys i have same problem and this is work very nice for me :)
Vector3 diff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
diff.Normalize();
float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 90);
i hope this work for you :)
this isn't working for me, the sprite i'm having turn is moving and the farther it gets from the center the more uncontrollable it is, and the rotations seem to be reversed, up and down at right, but left and right are flipped. the only difference is my diff is the target - transform.postion where target is a vector3 of the object i'm trying to chase
Do not forget to view the answer at the bottom provided by @abar, if you need a simpler way.
Answer by abar · Feb 12, 2016 at 05:25 PM
Surely the simplest solution is:
transform.right = target.position - transform.position;
Maybe that hasn't always been possible, but it sure beats messing about with Atan2.
This made my day, why isn´t this the accepted answer??? Thank you so much!
this code helped me half way. this code make the gameobject rotation to the target. but how can i make it move to the target?
transform.position = Vector2.$$anonymous$$oveTowards(transform.position, target.transform.position, speed * Time.deltaTime); transform.right = target.position - transform.position;
It is too genius that I have to login my account and give you a vote!! This is really easy to understand and it makes me able to $$anonymous$$ch my students the 2DLookAt function on my lesson!! Thanks a lot!!
Great solution! Also, if you (like me) want the turning of the object to be gradual you can use Lerp or Slerp like this:
transform.up= Vector3.Lerp(transform.up, (target.position - transform.position), turnRate);
Great answer!
As an additional hint: if you use unity 2d view, please use transform.up
than transform.right
.
This depends on which way "forward" is for you. If forward is to the right, as is convention, then transform.right
is still correct.
Answer by robertbu · Feb 05, 2014 at 05:14 PM
Given the right side of your sprite as the forward, and having a 'target' as a Transform (your code has it as a game object), you can do it this way:
Vector3 dir = target.position - transform.position;
float angle = Mathf.Atan2(dir.y,dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
I know it is a bit late, but is it possible to make sprite rotate for a certain degree? I am using On$$anonymous$$ouseDrag(), and I don't want it to juse follow mouse, but to skip 15 degrees.
I like your solution for it's simplicity and in this way you don't have to use .Normalize. What I did to make this piece of code result in a sprite really pointing forward to the target was simply to increase the angle with 270f. So you get:
Vector3 dir = target.position - transform.position;
float angle = $$anonymous$$athf.Atan2(dir.y,dir.x) * $$anonymous$$athf.Rad2Deg + 270;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
Answer by mustafa · Feb 05, 2014 at 04:53 PM
// LookAt 2D
Vector3 target;
// get the angle
Vector3 norTar = (target-transform.position).normalized;
float angle = Mathf.Atan2(norTar.y,norTar.x)*Mathf.Rad2Deg;
// rotate to angle
Quaternion rotation = new Quaternion ();
rotation.eulerAngles = new Vector3(0,0,angle-90);
transform.rotation = rotation;
This solution worked for my relative aim assist script.
Thank You!!!!!!! I went crazy because of my enemies didn't follow the Player. They disappeared!
Then I saw that Transform.LookAt (...) caused the bug and now I know how to fix it. Thanks!!
Answer by YoungDeveloper · Nov 27, 2013 at 07:35 PM
You could use vector2, it have only x and y coordinates.
Na, No Luck, just tried...
Vector2 LookAtPoint = new Vector2(Target.transform.position.x, Target.transform.position.y);
transform.LookAt(LookAtPoint);
Is This what you mean?
@LEDWOR$$anonymous$$S
That works fine for me. $$anonymous$$ake sure to keep your plane in $$anonymous$$d. If you're working with the XY plane then that should work fine, but if you're working on the ZY plane then you'll have to do
Vector2 LookAtPoint = new Vector2(Target.transform.position.z, Target.transform.position.y);
transform.LookAt(new Vector3(0,LookAtPoint.y,LookAtPoint.x);