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
1
Question by rodrigozol · Jun 01, 2013 at 08:08 PM · rotationeulerangles

Identify range of rotation / eulerAngles - Not working for backward

I want to identify the range of rotation of an object. Looking forward works, but not backwards. What is the tip for work? Why rotation.eulerAngles not working in this case? Tks.

 var quantRot : float;
 
 function Update () {
 
  transform.eulerAngles.y = quantRot;
 
  if (transform.rotation.eulerAngles.y >= 270f || transform.rotation.eulerAngles.y <=90f){ // works
  
      print ("Car looking to north in the world");
  }
  
   if (transform.rotation.eulerAngles.y <= 271f || transform.rotation.eulerAngles.y >=91f){ // doesn't work
  
      print ("Car looking to south in the world");
  }
 }
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

Answer by robertbu · Jun 01, 2013 at 08:18 PM

Reading from eulerAngles and expecting a specific value often fails. That's because there are multiple eulerAngle representations for a physical representation, and you cannot depend on on Unity to maintain any single representation. For example if you set your rotation to (180,0,0) in a script, and then use a Debug.Log() to print the rotation out, you will get (0,180,180)...the same physical rotation but represented differently.

Given your use of 270 and 90, I'm not sure of how you have your car setup with respect to what is forward. If you had a traditional car where forward was the side of the car facing positive 'z' when the roation was (0,0,0), and if north is positiive 'Z', then you can solve this problem using Vector3.Angle():

 if (Vector3.Angle(transform.forward, Vector3.forward) < some_angle) {
    // The car is headed north
 }

Vector3.Angle() returns an unsigned angle. 'some_angle' defines how close you have to be to pointing north before the code fires. A value of 90, would cover all 180 degrees.

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 rodrigozol · Jun 02, 2013 at 07:43 PM 0
Share

With "Vector3.Angle" also works. Tks all

avatar image
0

Answer by aldonaletto · Jun 01, 2013 at 08:42 PM

eulerAngles are tricky: always assign the XYZ values at once, and only trust in the Y value when X and Z are 0:

 function Update () {
  
  transform.eulerAngles = Vector3(0, quantRot, 0);
  if (transform.eulerAngles.y >= 270f || transform.eulerAngles.y <=90f){ // works
      print ("Car looking to north in the world");
  }
  if (transform.eulerAngles.y <= 271f || transform.eulerAngles.y >=91f){ // doesn't work
      print ("Car looking to south in the world");
  }
 }

Anyway, there's a better method to find whether the object is looking to world north or south:

 if (Vector3.Dot(transform.forward, Vector3.forward)>=0){
     print ("Car looking to north in the world");
 } else { 
     print ("Car looking to south in the world");
 }
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 rodrigozol · Jun 02, 2013 at 07:23 PM 0
Share

With "Vector3.dot" works perfect.

avatar image aldonaletto · Jun 02, 2013 at 11:09 PM 0
Share

The dot product returns a value proportional to the cosine of the angle between the two vectors: this value is positive when the absolute angle is below 90 degrees, and negative when it's higher (90 degrees gives exactly 0). This makes the dot product very useful when deciding if something is in front or behind the character:

 var targetDir = target.position - transform.position;
 if (Vector3.Dot(targetDir, transform.forward) > 0){
   // target is in front of this object
 } else {
   // target is behind this object
 }

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

15 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

Related Questions

Code to rotate around a local axis until local Y is 0? 0 Answers

Rotating a rigidbody on mouse click. 1 Answer

Rotation on Android vs. PC 0 Answers

2D animation : problem with rotation interpolation 1 Answer

trying to RotateAround an object around another object using mouse 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