Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by Leroterr · May 11, 2014 at 01:40 PM · 2dpositionlookatfollowtop-down

How do I make an object look at an another object in Top-Down 2D? ( C# )

I've tried googling it but none seemed to work for me.

How can I make an object follow and LOOK AT an another object while they both move?

LookAt doesn't work properly in 2D.

Thanks! :)

/////

Here is my current code to follow the object. It doesn't contain any code to look at the object.

 public Vector3 objectPosition;
 private Vector3 diff;
 
 public static float speed = 5.0f;
 
 // Use this for initialization
 void Start () {
     
     transform.position = new Vector3 (0,25,0);
     
 }
 
 // Update is called once per frame
 void Update () {
 
     Vector3 playerPosition = GameObject.FindWithTag("object").transform.position;
     
     transform.position = Vector3.Lerp (transform.position, objectPosition, Time.deltaTime * speed);
     
 }
Comment
Add comment · Show 1
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 meat5000 ♦ · May 11, 2014 at 01:41 PM 0
Share

Try feeding in your 2D coords to the LookAt and zeroing the third, unused axis.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by robertbu · May 11, 2014 at 01:54 PM

Assuming standard 2D on the XY plane:

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

This code assumes that the front side of the object is to the right. If your front side is the top, then add 90 to 'angle' before passing it to AngleAxis().

Comment
Add comment · Show 5 · 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 Leroterr · May 11, 2014 at 02:04 PM 0
Share

Thanks! It worked perfectly.

If you have the time, could you explain to me how it worked? Like, line by line. Thanks again!

Anyone else could explain her code as well. Would be much appreciated :)

avatar image robertbu · May 11, 2014 at 03:20 PM 0
Share

The first line creates a direction vector. Vectors don't have an origin...they just point in a particular direction. If you don't understand vectors, spending an hour or two reading about them will really help you with Unity.

Imagine that Vector starting at the origin and pointing in a direction like the hand on a clock. The second line calculates the angle of that "clock hand." Angle 0.0 is directly to the right (three o'clock). Up (noon) is 90 degrees.

AngleAxis() spins an object around a particular axis. I'm using Vector3.forward, so it will spin the object on the world's 'z' axis. I'm rotating to the angle we calculated in the previous line.

avatar image Leroterr · May 11, 2014 at 03:53 PM 0
Share

Thank you so much Robert! :D <3 <3 <3

avatar image alexjhones286 · Jan 23, 2017 at 01:43 AM 0
Share

Thank you, Brazil here.

avatar image paulo-developer · Jan 23, 2017 at 01:44 AM 0
Share

Hello friend, thank you very much for the solution, thank you from Brazil.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Camera rotation around player while following. 6 Answers

Waypoint navigation in 2D without LookAt 2 Answers

Shoot second cell in 2d 0 Answers

Problem With 2D Camera Following Target 3 Answers

Main Camera child under player, causes flip 3 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