motion *= (Mathf.Abs(inputVec.x) == 1)?.7f:1;
as far as i know, the line means, that variable motion gets multiplied by the absolute value of x part of vector inputVec, but i don't understand what happens next.
motion *= (Mathf.Abs(inputVec.x) == 1)?.7f:1;
as far as i know, the line means, that variable motion gets multiplied by the absolute value of x part of vector inputVec, but i don't understand what happens next.
If the condition Mathf.Abs(inputVec.x) == 1
is true, then motion
is multiplied by .7f. Otherwise by 1.
The question mark is the conditional operator. It is a compact way to write an if else statement.
For, instance this:
if(Mathf.Abs(inputVec.x) == 1)
{
motion *= .7f;
}
else
{
motion *= .5f;
}
is equivalent to this:
motion *= (Mathf.Abs(inputVec.x) == 1)?.7f:.5f;
So you can write one line of code instead of 8 !
the ?:
operator is a short way for a if
/else
it's called conditional operator
the *=
operator is the shortcut for x = x * 1
, explained here
Math.Abs()
returns the absolute value for a given value
0.7f
- the f
is a suffix that declares the value as a float type
so..
motion *= (Mathf.Abs(inputVec.x) == 1)?.7f:1;
equals
if (Mathf.Abs(inputVec.x) == 1)) //if inputVec.x equals 1 or -1
{
motion = motion * 0.7;
}
else
{
motion = motion * 1;
}