Is there any analogue of the function Mathf.Clamp for comparison? Those. to return true if the value is not more than the specified maximum and not less than the minimum. Analog about here such scribbling

if( ar.x > xmax && ar.x < xmin && ar.y > ymin && ar.y < ymax && ar.z > zmin && ..) 

How to write this shorter?

  • Need for a scalar (one component) or for a vector (2+ components at once)? - Kromster
  • Mathf from UnityEngine not suitable? - eastwing
  • @Kromster I compare Input.mouse position (Vector3) and Vector2, so I’ll probably have to compare one component) - Dmitrii
  • @eastwing I mentioned it in a post. Explain how you use it - Dmitrii
  • Ok, I misunderstood the question. There is another idea, now I will describe in response - eastwing

1 answer 1

You can write an extension method for Vector3, for this you need to declare a public method inside the non-nested static public class. Like that:

 public static class Extender { public static bool Clamp (this Vector3 source, float[] Comparers) { return source.x > Comparers[0] && source.x < Comparers[1] && source.y > Comparers[2] && source.y < Comparers[3] && source.z > Comparers[4] && source.z < Comparers[5] } } 

Then, directly in the handler, the record will shrink to:

 if (ar.Clamp(new float[] {minX, maxX, minY, maxY, minZ, maxZ})) 

The example is rough, you can make it more beautiful and readable, but the idea illustrates

Learn more about extension methods.