- Home /
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
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)
Your answer
Follow this Question
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