• 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 Gafgarion · Dec 18, 2013 at 12:17 AM · rotate object4.3

Rotation going from 180 to 0 suddenly

Hello community, I have a question that have been messing with my brain these days.

I'm making a simple 2d character using the new 2d features(sprites). I'm rotating the character's arms and head so it always faces a target controlled by mouse input, and it works well, except for one issue: when the arms or the head (they are 2 separate GOs) rotation in Z are equal to 180 degrees, it suddenly goes to 0 in Y and Z, so it actually never can have 180º of rotation in Z, causing the sprite to be totally inverted. I use a sprite for the case where the target's .x is higher than the char's and one for the opposite, if it matters. alt text

Any help is strongly appreciated =D

 Vector3 tempRotHead = head.position - target.position;
 head.right = Vector3.RotateTowards(head.right, -tempRotHead, 30, 30);
 
 Vector3 tempRotGun = gun.position - target.position;
 gun.right = Vector3.RotateTowards(gun.right, -tempRotGun, 30, 30);

This is the code I'm using to rotate the objects.

print.png (496.6 kB)
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
0
Best Answer

Answer by robertbu · Dec 18, 2013 at 06:12 AM

I have a bit of a guess here. With Quaternions, sometimes they meet the letter of function, but they end up flipping things. That is, at the end of your RotateTowards(), gun.right and -tempRotGun are aligned, though the object is flipped in the process. Here is a bit of alternate code for rotating an object to aim at a target with a bit of smoothing. It will not flip the object. It expects that when the rotation is (0,0,0), the object will point to the right. You'll have to evaluate this code, and then figure out how to change your code to match. To test, put it on an sprite with the "pointer" pointing right, drag and drop the target onto the 'target' variable in the inspector, hit play, and move the target around in the inspector.

 #pragma strict
 
 var target : Transform;
 var speed = 180.0;
 
 private var qTo : Quaternion;
 
 function Update () {
     var lookPos = target.position - transform.position;
     var angle : float = Mathf.Atan2(lookPos.y, lookPos.x) * Mathf.Rad2Deg;
     qTo = Quaternion.AngleAxis(angle, Vector3.forward);
     transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, speed * Time.deltaTime);
 }
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 Gafgarion · Dec 18, 2013 at 01:10 PM 0
Share

Thanks! This solved the problem almost entirely! The only adaptation was to remove the deltaTime, so it doesn't gets smooth, but precise. Thanks again!

avatar image robertbu · Dec 18, 2013 at 04:29 PM 0
Share

If you don't need deltaTime, then you don't need qTo. Just:

 function Update () {
     var lookPos = target.position - transform.position;
     var angle : float = $$anonymous$$athf.Atan2(lookPos.y, lookPos.x) * $$anonymous$$athf.Rad2Deg;
     transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
 }

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

17 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

Related Questions

Bad Input Values for Rotation on Object with 5+ Children 0 Answers

Rotating GameObject with transform.Rotate issue! 1 Answer

Object spinning around it's own axis whilst travelling in a straight line 1 Answer

iTween Constant Rotation 3 Answers

How do I make rotating objects collide? 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