Greetings,
Please correct me if I’m wrong, but…
On the documentation page it says that Vector3.Min returns a vector that is made from the smallest components of two vectors.
However, in my opinion this is not happening.
The example below:
public Vector3 a = new Vector3(-10, 0, 2);
public Vector3 b = new Vector3(0, 0, 0);
print(Vector3.Min(a, b));
returns “-10.0, 0.0, 0.0”
This is not the smallest components. The x component -10 is the largest one, not the smallest. I would have expected it to return “0.0, 0.0, 0.0”.
I can see that Vector3.Min follows the same logic as Mathf.Min. But in Vector world the smallest negative number is not the smallest vector. So, I would have expected a different logic here.
Question: If I have after all misunderstood this function, how I can get the true smallest components of two vectors, meaning the ones that are closest to zero?
Thanks
If you are going to go with lots of negative numbers, you can use Mathf.Abs to always get the positive numbers. Thus, your minus but bigger numbers ( bigger in the vector world ) will be calculated as positive numbers and return the smallest ones correctly.
public Vector3 a = new Vector3(Mathf.Abs(-10), 0, 2);
public Vector3 b = new Vector3(0, 0, 0);
And if your code won’t know which numbers will be negative and which will be positive, then you need your generated numbers to the variables, and always write your Vector’s components using Mathf.Abs.
Moreover, for the “logic” part, I don’t know definetely, but it can be because Unity compares the components of the vectors, which are simply float values. I think the process & calculations are done over the Math side not the Vector side. Ofcourse, it would be better if someone who knows the deal could give us a certain explanation for the logic.
If you think of the Math behind it, (A Vector3 is just 3 floats) then technically -10 is the minimum. Sure 0 is closer to (0,0,0) but if you think of a float on the scale of an infinite number line, then -10 would be closer to -infinity than 0. So in a sense you can think of a Vector3 like (-infinity, -infinity, -infinity) as the absolute “smallest” vector.
So the first solution you might think is to use Vector3.magnitude.
if (a.magnitude > b.magnitude)
smallestVector = a;
else
smallestVector = b;
But this doesn’t return a new Vector. And it also doesn’t take into account all 3 components individually. Say for example you had a Vector (0, 2, -10) and Vector (0, 0, 20) - you would get (0, 2, -10). It would have excluded the 0 because magnitude only returns a float–and we’re just setting the old vectors.
But let’s break the problem down further. You want essentially to get the magnitude of each individual component of Vector3. Luckily for you, we have Mathf.Abs. It returns the absolute value of any float. But we don’t a Vector3.Abs!! So we need to make our own:
function AbsoluteVector (vec : Vector3);
{
var newVec = Vector3(Mathf.Abs(vec.x), Mathf.Abs(vec.y), Mathf.Abs(vec.z));
return newVec;
}
Now we simply call the Vector.Min again, but with our new absolute vectors:
Debug.Log(Vector3.Min(AbsoluteVector(a), AbsoluteVector(b)));
Now you’ve returned a new vector that uses the absolute value of each component instead of allowing negatives!
0 is larger than -10 yes, but if you’re dealing with Vectors it’s easy to confuse smallest with the shortest. I was surprised to find we didn’t have a Vector3.Abs function. But I guess if I was able to write one in 5 seconds then it’s not really much of a time saver anyways 
Thanks for the feedback!
I will try mathf.abs method next.
Ok, this is what I came up with:
Vector3 ShortestVector (Vector3 a, Vector3 b) {
Vector3 absA = new Vector3(Mathf.Abs(a.x), Mathf.Abs(a.y), Mathf.Abs(a.z));
Vector3 absB = new Vector3(Mathf.Abs(b.x), Mathf.Abs(b.y), Mathf.Abs(b.z));
Vector3 comp = new Vector3(absA.x < absB.x ? a.x : b.x, absA.y < absB.y ? a.y : b.y, absA.z < absB.z ? a.z : b.z);
return comp;
}
My original question was a bit silly though. How can you return a vector with shortest components when there is a case like this: (-1,-1,-1) and (1,1,1). They are equal length but opposite direction. So the conclusion is, this can’t be done reliably.