using System;
public class SortingVectors : MonoBehaviour {
public Vector3[] playerInfo;
void Start ()
{
playerInfo = new Vector3[8];
}
void Update ()
{
Array.Sort(playerInfo, Vector3Compare);
}
private int Vector3Compare(Vector3 value1, Vector3 value2)
{
if (value1.x < value2.x)
{
return -1;
}
else if(value1.x == value2.x)
{
if(value1.y < value2.y)
{
return -1;
}
else if(value1.y == value2.y)
{
if(value1.z < value2.z)
{
return -1;
}
else if(value1.z == value2.z)
{
return 0;
}
else
{
return 1;
}
}
else
{
return 1;
}
}
else
{
return 1;
}
}
}
This arranges the Vector3's based on the X value first, Y second, and then Z.
While this may be very straightforward to many of you, it wasn't until I wrote this out that I fully understood what declaring an int/function actually allows you to do (I probably still don't fully understand).
I hope this helps someone and I believe people will find other applications for this, even with Vector2's or Vector4's.
Thanks for being awesome!
God bless!
Howey
↧