I am looking at somebody code. I found:
XOffset = !MirroredMovement ? trans.x * MoveRate : -trans.x * MoveRate;
He use '?' character, why ? What does it mean ? I did not understand.
I am looking at somebody code. I found:
XOffset = !MirroredMovement ? trans.x * MoveRate : -trans.x * MoveRate;
He use '?' character, why ? What does it mean ? I did not understand.
?
is not the operator, the combination of ?
and :
is the operator, called 'ternary operator'.
The ternary operator is an operator that takes three arguments. The first argument is a comparison argument, the second is the result upon a true comparison, and the third is the result upon a false comparison. If it helps you can think of the operator as shortened way of writing an if-else statement.
That's the Ternary Operator.
condition ? value1 : value2
Its short for:
if (condition)
{
return value1;
}
else
{
return value2;
}
In this example you could write this:
XOffset = !MirroredMovement ? trans.x * MoveRate : -trans.x * MoveRate;
like that:
if (!MirroredMovement)
{
XOffset = trans.x * MoveRate;
}
else
{
XOffset = -trans.x * MoveRate;
}