Reputation: 66
I want to remove 10 from Vector3 diff
x
& z
if is is < 0
, and add 10 if it is >= 0
. Is there a better way to do that?
if (diff.x < 0)
diff.x -= 10;
else if (diff.x >= 0)
diff.x += 10;
if (diff.z < 0)
diff.z -= 10;
else if (diff.z >= 0)
diff.z += 10;
Upvotes: 0
Views: 901
Reputation: 4431
If your numbers are doubles, you could use Math.CopySign. Math.CopySign(x,y) has the magnitude of x and the sign of y.
diff.x += Math.CopySign( 10, diff.x);
diff.z += Math.CopySign( 10, diff.z);
Upvotes: 0
Reputation: 142
You could use the conditional operator to only make a single comparison for both x and z. Again, people can have many different opinions about conciseness, readability, etc.
diff.x += (diff.x < 0 ? -10 : 10);
diff.z += (diff.z < 0 ? -10 : 10);
Upvotes: 1