Reputation: 31
I'm not very experienced in unity C# so this may be something that is very easy that I'm over complicating.
what I'm trying to achieve is to get a variable to set to something depending on if 2 objects have similar rotations, but its outputting bullet1 every time.
if(hand1.transform.rotation.y > (hand2.transform.rotation.y + 90)
&& hand1.transform.rotation.y < (hand2.transform.rotation.y - 90))
{
bulletresult = bullet1;
}
else
{
bulletresult = bullet2;
}
Upvotes: 1
Views: 1634
Reputation: 90749
Transform.rotation
is a Quaternion
(see also Wikipedia - Quaternion)!
Except you know exactly what you are doing - which you don't, no offense ;) - NEVER directly get or set the individual components of a Quaternion
!
So in case you didn't know: A Quaternion
has not 3
but 4
components x
, y
, z
and w
. All of them move in the range -1
to 1
.
Your condition can never be true.
You could check using Quaternion.Angle
if(Quaternion.Angle(hand1.transform.rotation, hand2.transform.rotation) <= 90)
but be aware that this returns the rotation delta on any axis.
Alternatively for getting the delta only on the global Y-Axis you could rather use vectors and flatten the individual right (local X) or forward (local Z) vectors onto the XZ plane and use Vector3.Angle
// get the local right vectors in global space
var right1 = hand1.transform.right;
var right2 = hand2.transform.right;
// flatten the vectors so we don't have any difference in the global Y axis
right1.y = 0;
right2.y = 0;
// Now get the angle between these
if(Vector3.Angle(right1, right2) < 90)
Upvotes: 0
Reputation: 141
You can you the function float Angle(Quaternion a, Quaternion b) to find out the angle between to rotation. And please check your condition again. It will never be true. (Tell me if I am wrong)
Upvotes: 0
Reputation: 684
You need to use hand1.tranfsform.rotation.eulerAngles.y
instead of hand1.tranfsform.rotation.y
. Same of course for hand2
Upvotes: 1