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 chilboy · Nov 13, 2012 at 11:57 AM · c#rotationrotation detection

why won't my script do a proper rotation check

so i am making this bowling game and decided that i want to destroy de pins after they had fallen over. but to let them fall down correctly and hit other pins as well i put a timer on there for 10 seconds. but somehow the timmer won't start. it starts when i put in there if the z or the x rotation are not equall to zero but because of the gravity do they shift position a little bit so it will start automaticly. to avoid this i've put in that the x or z rotation should be more than or equal to 1 or less than or equal to -1. i've checked by a rotation of only 1 degree they will fall over. here is my code (it is in c#):

 using UnityEngine;
 using System.Collections;
 
 public class pins_fall_over : MonoBehaviour {
     
     
      public float time_passed;
         
     // Use this for initialization
     void Start () {
     
         time_passed = 0;
         
     }
     
     // Update is called once per frame
     void Update () {
     
         float x_rotation = transform.rotation.x; 
         float z_rotation = transform.rotation.z; 
         
         if( x_rotation >= 1.0f || x_rotation <= -1.0f || z_rotation <= -1.0f || z_rotation >= 1.0f)
             time_passed += Time.deltaTime;
         
             
         if(time_passed >= 10)
         {
             Destroy (this.gameObject);
         }    
         
     }
 }



p.s. time_passed is public so i could see it more easely in the inspector

p.p.s i'm sorry if my grammar is bad because english is not my original language

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

Answer by aldonaletto · Nov 13, 2012 at 12:23 PM

transform.rotation is a Quaternion, not those nice angles we see in the Inspector (they are actually transform.localEulerAngles). You could try to use transform.eulerAngles, but its values are not so reliable for this purpose - there are several XYZ combinations equivalent to any possible rotation, and you may be unlucky and get the wrong combination.
A better way to check tilting of a vertical object is to verify its transform.up vector: since it's normalized, the Y component is numerically equal to the sin of the elevation angle - it should be 1 for a completely vertical object, and < 0.999 for an object inclined more than 2.5 degrees in any direction:

 ...
 void Update () {
    if (transform.up.y < 0.999f){ // 
      time_passed += Time.deltaTime;
    }
    if(time_passed >= 10){
      Destroy (this.gameObject);
    } 
 }

If you want less than 2.5 degrees, the value to compare to Y may be calculated from sin(90 - maxDegrees)

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 chilboy · Nov 13, 2012 at 12:35 PM 0
Share

thank you, i've tried this and it works

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

10 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

Related Questions

Flip over an object (smooth transition) 3 Answers

Camera rotation stop if x is 50 2 Answers

Distribute terrain in zones 3 Answers

Know if an object has been rotating 180 or 360 degrees? 0 Answers

Multiple Cars not working 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