- Home /
How is this operator in a variable declaration?
Have noticed this line in one of the bundled Unity scripts. Am not familiar with the use of comparison operators when setting a variable. Can anyone explain what it means?
movement.isMoving = Mathf.Abs (inputDirection) > 0.1;
Answer by flamy · Feb 02, 2012 at 01:55 PM
movement.isMoving is a boolean variable, You are checking whether the value of input direction is more than 0.1 either in negative or positive...
basically Mathf.Abs(inputDirection)>0.1 is a condition so it will return either true or false to the variable isMoving. This is equal to
if(Mathf.Abs (inputDirection) > 0.1)
{
movement.isMoving=true;
}
else
{
movement.isMoving=false;
}
Aditional: Check ternary operator is more like a simple if else loop that could also be used during variable declaration, with a condition. eg: var x=(y<3?3:3);
this will clamp the value to 3 if the value is 3 or more or you will get the value of y in x. Hope it is clear :)
Thanks for this answer, @flamy, I was curious too about this. Just a question: is it possible both in Unityscript and C#?
I ask because I've never seen this synthax.
yes it is in any language. it is not specific to unityscript or c#....
but prefer not to use it though, because it would make the code hard to read sometimes.
Your answer
Follow this Question
Related Questions
How to properly declare this variable 2 Answers
How do you clear a variable in the inspector? 4 Answers
variable doesn't change 1 Answer
Why the variable updates only twice? 1 Answer