How do I check for “Infinity”?
Can I make an if statement for checking if the variable is returning infinity?
if ( myVecto.x == infinity ) { ....
You can check if is returning infinity by printing in the console first. For example:
if ( vectorForce.x > 100000f) Debug.Log( vectorForce.ToString());
And it will print in console something like this:
(Infinity, -Infinity, -Infinity)
But I can’t make something like this
if (vectorForce.x == “Infinity”) { <<<------------ RETURNS ERROR
Because my rigid body force assign attempt for ‘myGameObject’ is not valid. And the Input force is
{ Infinity, -Infinity, -Infinity }
And/or transform float or Vector assign attempt for my gameObject is not valid.
Input localScale is { Infinity, 0.500000, 0.500000 }.
If you don’t care if the value is PositiveInfinity orNegativeInfinity you can simply use IsInfinity
if(vectorForce.x.IsInfinity())
However if you want to distinguish between positive and negative infinity you would simply use:
if(vectorForce.x == float.PositiveInfinity)
// or
if(vectorForce.x == float.NegativeInfinity)
IsInfinity will return true in both cases. Note: if the infinity value isn’t expected by you, you might want to investigate where it actually comes from. Common causes are that you divided your vector by “0”. Dividing a value different from 0 by zero doesn’t result in an error but in positive or negative infinity.
So in general it’s better to handle that situation before you divide. Note that when you divide 0 by 0 it will result in a NaN value instead as 0/0 is not defined.
I don’t quite understand the use case, but you can test whether a float value is at its max possible using:
if(vectorForce.x == float.MaxValue)