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 TenTonApe · Mar 22, 2014 at 05:05 PM · 2dspriterotate objecttrigonometrylook at

2D character face another character

So heres what I'm trying to do. I want a 2D object that will look at another 2D object, and when that object passes to the other side of it, it will flip and continue to look at the target so that it never rotates upside down. So if I was putting this code on a 2D characters head they would look up as the target passed over head, but then flip and start looking more downward as it passed by instead of the head rotating upside down.

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

2 Replies

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

Answer by robertbu · Mar 22, 2014 at 05:42 PM

Here is a bit of code that does what I believe you want. Setup:

  • The object must be a sprite or other object that is rendered on both sides

  • The 'front' of the object needs to be on the right when rotation is (0,0,0)

  • Select the sprite texture and set the anchor to 'Right'

  • After attaching your script, select the game object with the script and drag and drop the target object on the 'target' variable in the Inspector.


    pragma strict

    var target : Transform;

    function Update () { var dir = target.position - transform.position;

       var q : Quaternion;
         if (dir.x >=0) {
             q = Quaternion.identity;
         }
         else {
             q = Quaternion.Euler(0,180,0);
             dir.x = -dir.x;
         }
         
         var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
         transform.rotation = q * Quaternion.AngleAxis(angle, Vector3.forward); 
     }
    
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 TenTonApe · Mar 22, 2014 at 06:09 PM 0
Share

Awesome thank you.

avatar image
0

Answer by Destran · Mar 22, 2014 at 05:14 PM

Try This:

 var rotationSpeed : float = 5.0f;
 
 functon Start()
 {
     rotationSpeed = 5.0f;
 }
 
 
 function Update()
 {
     FacePlayer();
     // rotate to look at player
     transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position, transform.positon), rotationSpeed * Time.deltaTime);
 }
 
 
 
 function FacePlayer ()
 {    
 var relativePoint = transform.InverseTransformPoint(target.transform.position);
     if (relativePoint.x < 0.0)
     {
         transform.Rotate(0,180,0);
     }
     else 
     {
         transform.Rotate(0,0,0);
     }
 }


Edit: Looking back at your original question this might not be exactly what you wanted, but if you want something with a different functionality let me know and I'll try to help.

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 TenTonApe · Mar 22, 2014 at 05:42 PM 0
Share

The

 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position, transform.positon), rotationSpeed * Time.deltaTime);

Line doesn't work at all, the object points into the Z axis and twitches a bunch.

avatar image Destran · Mar 22, 2014 at 05:45 PM 0
Share

sorry that should be transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.positon), rotationSpeed * Time.deltaTime);

But, robertbu posted, and he's almost always perfect so.

avatar image TenTonApe · Mar 22, 2014 at 06:09 PM 0
Share

robertbu was indeed perfect, thanks anyway.

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

Top Down Sprite Facing Mouse 2 Answers

Ground checking with a frequently rotating player 2 Answers

Find point of top of rotated sprite 1 Answer

Rotating a Sprite to face mouse / target? 2 Answers

How can I achieve this particular 2D lighting effect? 2 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