• 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 M.A.N. · Dec 06, 2013 at 07:23 PM · rotationgameobjectquaternionrotate

How get the rotation of an GameObject?

Hello, I want to know how I can get the rotation of an Object:

In particular I have a rectangle that rotate of 45° itself if the mouse is clicked. But I want to do something(for example: Debug.Log(You Win!)) if it is rotated of 180°. Do you know any solution for this problem?

Thanks in advance.

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
2
Best Answer

Answer by robertbu · Dec 07, 2013 at 05:52 AM

Eric5h5's solution makes the most sense given your question. As an alternate, you can check the Vector3.Dot() of two vectors. For example, if your rectangle is rotating around the 'z' axis, and it starts with it local 'up' pointing in the same direction as world 'up', you can check with:

 if (Vector3.Dot(transform.up, Vector3.down) > 0.99) {
     Debug.Log("You Win");
 }

If the initial rotation is arbitrary you can do something like:

 private var down : Vector3;
 
 function Start() {
     down = -transform.up;
 }
 
 function Update() {
     if (Vector3.Dot(transform.up, down) > 0.99) {
         Debug.Log("You Win");
     }
 }
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 M.A.N. · Dec 12, 2013 at 02:35 PM 0
Share

Ok, this script works very good! But if I want to modify the degrees from 180 to 90 or another value,how can I to do(there is table)?

avatar image robertbu · Dec 12, 2013 at 03:10 PM 0
Share

For the Dot product of two, normalized vectors:

  • If they are going in the same direction, the value will be 1.0.

  • If they are 90 degrees from each other the value will be 0.0.

  • If they are in opposite directions from each other the, the value will be -1.0.

You don't want to compare floating point values with an == sign, and I don't know how accurate your rotation code is. If you have a specific angle in $$anonymous$$d, then you just find or generate a vector for that direction. For example, let's say you want a 90 rotation so that up is point right and you are rotating your object on the 'z' axis:

 private var compare : Vector3;
  
 function Start() {
     compare = transform.right;
 }
  
 function Update() {
     if (Vector3.Dot(transform.up, compare) > 0.99) {
         Debug.Log("You Win");
     }
 }

Alternately if you want 90 either to the left or to the right, you could do:

 private var compare : Vector3;
  
 function Start() {
     compare = transform.up;
 }
  
 function Update() {
     var dot = Vector3.Dot(transform.up, compare)
 
     if (dot > -0.01 && dot < 0.01) {
         Debug.Log("You Win");
     }
 }
avatar image
0

Answer by juippi112 · Dec 06, 2013 at 07:33 PM

Easy:

 private var x : float;
 private var y : float;
 private var z : float;
 
 function Update()
 {
 x = transform.rotation.x;
 y = transform.rotation.y;
 z = transform.rotation.z;
 
 if(y == 180)
 {
     print("You won!");
 }
 }
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 Eric5h5 · Dec 07, 2013 at 05:08 AM 0
Share

Transform.rotation is not degrees, it's a 4D quaternion which contains x/y/z/w elements that range from -1.0 to 1.0 and do not directly correspond to world axes. Also you would never use direct float comparisons regardless, since it's quite likely that it will be not quite 180°.

Leaving all that aside, why would you make three global variables when you could just check transform.rotation.y? (If that worked, which it doesn't....)

avatar image
2

Answer by Eric5h5 · Dec 07, 2013 at 05:13 AM

I'd recommend using a script on the rectangle that holds the number of times it was clicked. Since each click is 45°, then a click count of 4 would be 180°. So check for clickCount == 4 (and make sure to wrap clickCount back to 0 when it reaches 8, since that's 360°).

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

19 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

Related Questions

Rotate an object relative to Camera 2 Answers

Rotating a GameObject based on another GameObject's y-axis 1 Answer

How do I make create a rotateable object that follows a raycast hit point? 2 Answers

Rotate around a locally tilted axis?, 1 Answer

Player character X rotation going haywire on game start 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