Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by NinjaRubberBand · May 16, 2014 at 08:19 AM · 2dtransformpoint

transform.LookAt(target); in 2D?

You probabaly knows what the line does in the title. It makes the attached object point at the "target" which is a variable. Well it works fine in 3d, but not in 2d. In 2D, the game is in layers, so if i want it to point at something which is some layers behind the "pointer" it rotates against it. But i only want it to point at the object in a 2d perspective, so it only rotates on the Z axis. How do i do that?

By the wat the "pointer" is a arrow that shows you were to go.

Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by iqbalklv · Oct 19, 2018 at 12:19 PM

I think this is the best one.. insert the tag of the object that u want to look at and play with the offset

  public string Tag;
     public float offset;
         
             private Transform target;
             private Vector3 targetPos;
             private Vector3 thisPos;
             private float angle;
         
          void Start () 
                {
                 target = GameObject.FindGameObjectWithTag(Tag).GetComponent<Transform>();
             }
         
          void LateUpdate()
             {
                 targetPos = target.position;
                 thisPos = transform.position;
                 targetPos.x = targetPos.x - thisPos.x;
                 targetPos.y = targetPos.y - thisPos.y;
                 angle = Mathf.Atan2(targetPos.y, targetPos.x) * Mathf.Rad2Deg;
                 transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle + offset));
             }
Comment
Add comment · 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 juanitogan · Aug 30, 2019 at 10:52 PM 0
Share

offset? The x and y are backwards and negate the x.

avatar image iqbalklv juanitogan · Jul 13, 2020 at 06:08 PM 0
Share

the offset is used just in case your sprite doesn't look "up"

avatar image
1

Answer by VesuvianPrime · May 16, 2014 at 08:44 AM

You can flatten your lookat position in the appropriate axis:

 Vector3 targetPos = target.transform.position;
 Vector3 targetPosFlattened = new Vector3(targetPos.x, targetPos.y, 0);
 transform.LookAt(targetPosFlattened);
Comment
Add comment · 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
1

Answer by burnsky · Apr 12, 2021 at 11:17 AM

There's an amazingly simple solution in this thread (NOT the one marked as 'best' but the one below with the many votes!)

Comment
Add comment · Show 1 · 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 d2clon · Feb 28 at 07:44 PM 0
Share

I save you a click:

 transform.right = target.position - transform.position;

Just be sure target and transform are in the same Z, if not you may have to flat this coordinate.

avatar image
0

Answer by BVGuled · Mar 09 at 04:32 AM

first you have to take the direction to target gameobject then use Quaternion.FromToRotation(Vector3.up, direction) : the first parameter is the current gameobject's up direction and second parameter is the direction to the target gameobject.

  public Transform target;
 
     private void Update()
     {
         Vector2 direction = target.position - transform.position;
         transform.rotation = Quaternion.FromToRotation(Vector3.up, direction);
     }

Comment
Add comment · 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

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

27 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

Related Questions

Stop Moving When Not Touching Screen? 1 Answer

Instantiated Prefab Facing Wrong Way 0 Answers

Shoot second cell in 2d 0 Answers

How to use SimpleMove randomly move an NPC? 1 Answer

Please Help - 2D Collisions Without Unity's Physics 1 Answer


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