- Home /
[SOLVED] Receive a float from a quaternion?
I'm attempting to find the x angle from an objects rotation, but it keeps telling me I can't be used on operands of type bool and float. I'm trying to use this in an if statement to determine the direction a player is facing. Ill leave the code below. Thank you to anyone that can help!
private void detectDirection()
{
if(-45f <= flashlight.transform.rotation.x <= -90f) //up
{
dirup = true;
dirdown = false;
dirright = false;
dirleft = false;
}
}
Answer by Mr_Admirals · Dec 26, 2018 at 11:09 PM
Well, first off, you're jumping the gun a bit on your if statement. The reason it's saying you can't compare a bool to a float is because if statements evaluate from left to right.
So the first thing to be evaluated will be the comparison:
-45f <= flashlight.transform.rotation.x
Let's say it returns true. Then the next thing to be compared in the if statement is:
true <= -90f
As you can see, that doesn't make much sense. What you want to do is split up those operations using the and operator. So it should look like:
if(-45f <= flashlight.transform.rotation.x && flashlight.transform.rotation.x <= -90f)
Second, quaternions don't behave in an innately predictable manner and more importantly, the unit of measurement is not degrees. What you're looking for is eulerAngles. The final code should then look like this:
private void detectDirection()
{
if(-45f <= flashlight.transform.eulerAngles.x && flashlight.transform.eulerAngles.x <= -90f) //up
{
dirup = true;
dirdown = false;
dirright = false;
dirleft = false;
}
}
Thank you so much, I feel dumb for forgetting to split up my operations but I definitely wont forget to in the future!
Your answer
Follow this Question
Related Questions
Unity thinks my Quaternion is a Float 2 Answers
How to shoot in the same direction my character is facing? 2 Answers
Quaternion rotation with normals bug: Only works into one direction? 1 Answer
Copy Rotation Direction only on Y Axis. 1 Answer
allows users to view 360º content without a VR headset 0 Answers