• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Dec 27, 2020 at 03:13 PM by Bunny83 for the following reason:

The question is answered, right answer was accepted

avatar image
45
Question by LEDWORKS · Nov 27, 2013 at 07:33 PM · 2dlookateuler

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.

Comment
Comments Locked · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Stonehead · Feb 11, 2014 at 09:51 PM 0
Share

Thanks so much. You just solved a problem I've been having for days!

avatar image diablia · Jul 11, 2014 at 10:56 PM 0
Share

thx dude. that rly helped

avatar image Zinge · Apr 03, 2015 at 12:29 AM 0
Share

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!

avatar image Fappp · Apr 25, 2015 at 09:16 AM 0
Share

transform.LookAt(Vector3(target.position.x , target.position.y , fixedZ));

avatar image Cladnic · Jun 20, 2015 at 12:53 PM 0
Share

I tried using this code to make a gameobject to "follow" my cursor

alt text

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

code.jpg (16.2 kB)
Show more comments

16 Replies

  • Sort: 
avatar image
74
Best Answer

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 :)

Comment
Comments Locked · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sandworm55 · Mar 03, 2015 at 05:28 AM 0
Share

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

avatar image deserdoo sandworm55 · Dec 15, 2019 at 07:39 PM 0
Share

Easier to use @abar 's function

avatar image LazyKnight · Jun 14, 2018 at 12:03 AM 4
Share

Do not forget to view the answer at the bottom provided by @abar, if you need a simpler way.

avatar image
191

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.

Comment
Comments Locked · Show 13 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Burkentain · Sep 02, 2016 at 06:15 PM 2
Share

This made my day, why isn´t this the accepted answer??? Thank you so much!

avatar image nikocr33nzz · Nov 22, 2016 at 09:13 AM 0
Share

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?

avatar image gonchostone nikocr33nzz · Sep 21, 2017 at 08:04 PM 0
Share

transform.position = Vector2.$$anonymous$$oveTowards(transform.position, target.transform.position, speed * Time.deltaTime); transform.right = target.position - transform.position;

avatar image thudme22 · Jan 12, 2018 at 03:17 AM 2
Share

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!!

avatar image Johan-Ed · Mar 19, 2018 at 02:31 PM 7
Share

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);

avatar image LazyKnight · Jun 14, 2018 at 12:04 AM 5
Share

Great answer!

As an additional hint: if you use unity 2d view, please use transform.up than transform.right.

avatar image nanodeath LazyKnight · Jun 02, 2019 at 07:10 PM 1
Share

This depends on which way "forward" is for you. If forward is to the right, as is convention, then transform.right is still correct.

Show more comments
avatar image
14

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);
Comment
Comments Locked · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image stfilip · Jul 10, 2014 at 11:18 AM 0
Share

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.

avatar image F-N · Jun 20, 2016 at 02:11 PM 2
Share

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);

avatar image
3

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;
Comment
Comments Locked · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nventimiglia · May 10, 2015 at 05:45 PM 0
Share

This solution worked for my relative aim assist script.

avatar image NikitaDemidov · May 11, 2019 at 01:09 PM 0
Share

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!!

avatar image
0

Answer by YoungDeveloper · Nov 27, 2013 at 07:35 PM

You could use vector2, it have only x and y coordinates.

Comment
Comments Locked · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LEDWORKS · Nov 27, 2013 at 07:43 PM 0
Share

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?

avatar image Paparakas · Nov 27, 2013 at 11:53 PM 0
Share

@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);
  • 1
  • 2
  • 3
  • 4
  • ›

Follow this Question

Answers Answers and Comments

73 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Animation does not start 1 Answer

Lookat player 2 Answers

topdown space game 1 Answer

How to define the "front" of an object? 0 Answers

2d obj look at limit in unity 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges