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 kamuzai1226 · Nov 17, 2014 at 09:09 PM · 2drotationspritelookattarget

2D LookAt not working as intended

So I have looked this up and found http://www.dphsw.co.uk/blog/?p=172, but when I try it out, it did not work for me so I had to add a line so that the sprite would be visible. There is still a problem with it, here is an image of the problem. What am I doing wrong? http://i.imgur.com/RDvks6O.png

 using UnityEngine;
 using System.Collections;
 
 public class Target : MonoBehaviour
 {
 
     private Transform target;
 
     private RaycastHit2D hit;
 
     // Use this for initialization
     void Start ()
     {
     }
     
     // Update is called once per frame
     void Update () 
     {
         if (!target)
         {
             hit = Physics2D.Raycast(transform.position, Vector2.up);
             Debug.DrawRay(transform.position, Vector2.up);
         }
 
         if (target)
         {
             hit = Physics2D.Raycast(transform.position, target.position - transform.position);
             Debug.DrawRay(transform.position, target.position - transform.position);
 
             var direction = target.position - transform.position;
 
             transform.LookAt(Vector3.forward, direction); //target has to be 0,0,1 in order for it to be visible.
             transform.rotation = new Quaternion(0, 0, transform.rotation.z, transform.rotation.w);
 
         }
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.tag == "Enemy" && !target)
         {
             target = other.gameObject.transform;
         }
     }
 
     void OnTriggerExit2D(Collider2D other)
     {
         if (other.gameObject.transform == target)
         {
             target = null;
         }
     }
 }
 

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

1 Reply

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

Answer by robertbu · Nov 17, 2014 at 09:24 PM

The best thing for a sprite is to make the texture so that the front is on the right. Then you can do this:

     if (target)
      {
          hit = Physics2D.Raycast(transform.position, target.position - transform.position);
          Debug.DrawRay(transform.position, target.position - transform.position);
 
          Vector3 direction = target.position - transform.position;
          float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
          transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
      }
Comment
Add comment · 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 kamuzai1226 · Nov 17, 2014 at 09:47 PM 0
Share

So using Atan2 is okay? I am planning on having like 500 of these things on the screen, would performance be okay using Atan2?

avatar image robertbu · Nov 17, 2014 at 09:53 PM 0
Share

There are other ways to solve this rotation, but I doubt they are any more efficient than this one. I doubt that Atan2() will play any role in performance issues you have with 500 objects.

avatar image kamuzai1226 · Nov 17, 2014 at 10:05 PM 0
Share

Alright thanks. This worked out great. Ty

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

26 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

Related Questions

LookAt() not working with mouse as target in 2D 1 Answer

2D Sprite Rotation 0 Answers

Joystick 2D Sprite Rotation and Movement 0 Answers

2D Sprite Animation - Rotate animation on axis 1 Answer

2D rotation question 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