- Home /
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.
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");
}
}
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)?
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");
}
}
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°).
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!");
}
}
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....)