• 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 superluigi · Feb 06, 2014 at 05:50 AM · 2dtransformsprite

Achieving something like LookAt but with the X axis.

I'm working on a 2D scene and have been trying to come up with a way to get the results of look at but on the X axis, that is my current objects right faces the target. I found a way around it by using parent objects but it's a jumbled mess. It gives me problems when I use colliders2D and mainly with sprites as these can't be seen if you rotate the objects. So I am wondering if some of you have had this problem before and if maybe you can lend me a hand.

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

3 Replies

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

Answer by ashique · Feb 06, 2014 at 06:06 AM

Calculate the direction between current Object and Target object e.g

Vector3 dir = (CurrentObject's Position - TargetObject position). normalized;

CurrentObject.transform.right = dir;

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 robertbu · Feb 06, 2014 at 06:24 AM 1
Share

In my experience, the direct assignment to transform.right sometimes results in the sprite flipping.

avatar image superluigi · Feb 08, 2014 at 12:32 AM 0
Share

There's only one problem with your code. When you use LookAt if the target goes behind the object, the object will turn around (do a 180) and continue looking at them. With your code the object just keeps on turning and is upside down if the target goes behind them. I'm working to find a solution to this last part.

avatar image
1

Answer by robertbu · Feb 06, 2014 at 05:56 AM

Since this is 2D and the right side of your object is consider the 'forward' side to be pointed at the target, 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
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 superluigi · Feb 06, 2014 at 11:30 PM 0
Share

There's only one problem with your code. When you use LookAt if the target goes behind the object, the object will turn around (do a 180) and continue looking at them. With your code the object just keeps on turning and is upside down if the target goes behind them. I'm working to find a solution to this last part.

avatar image robertbu · Feb 08, 2014 at 12:46 AM 0
Share

This rotation issue is inherit with a 2D. A fix is to detect when you are on the left side and change the texture. You can check left/right by checking the angle:

 if (angle > 90 || angle < -90) {
     Debug.Log("The target is on the left side of me");
 }

Another solution would be to convert the point to local coordinates and test:

 Vector3 local = transform.InverseTransformPoint(target.position);
 if (local.x < 0.0) {
     Debug.Log("The target is on the left side of me");
 }
avatar image superluigi · Feb 08, 2014 at 04:30 AM 0
Share

I'm gonna try it out. I know in the reference it says that LookAt points the objects up toward the worlds up vector so if your object starts turning upside down it'll do a 180 and fix it. I've been trying to replicate that.

avatar image
0

Answer by RyanZimmerman87 · Feb 06, 2014 at 06:00 AM

Might need more info but I think what you need is something like:

 //playerLookVector can be where you want to look
 
 //playerTransform is whatever object you want to look at the playerLookVector
 
 //lookObject is the object you want to lookAT
 
 
 //this.PlayerTransform.position.y will cause the player too look straight forward
 
 playerLookVector = new Vector3 (lookObject.transform.position.x, 
                                             this.playerTransform.position.y, lookObject.transform.position.z);
 
 this.playerTransform.LookAt(playerLookVector);


That's a 3d example but pretty sure you can still use it or do something similar with Vector2 instead of Vector3.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

How can I make a 2d sprite shoot the direction player is facing? 2 Answers

[2D] - Moving the child moves the parent <-- Unwanted behavior 1 Answer

Simultaneous Animation for an Object 0 Answers

transform.position not doing anything 1 Answer

Sprite is not shown moving 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