I have game objects that they’re moving clockwise in a circle path(this is actually how the game looks like in the end).and I need to find out these game objects arrangement .for example, if I have game objects which they are moving in the circle path(game objects name 1,2,3,4,5)may be in the end, the arrangement would like 2,4,1,3,5 (of course because the path is circular doesn’t matter what game object is the first one) or any other arrangement.how can I find this arrangement???
English isn’t my first language so if this is vague please ask me for more details.!
I haven’t tested the following script, I don’t have Unity right now. The idea is to define the function used to “compare” two gameobjects according to their position on the circle
// Drag & Drop the objects to sort in the inspector
public Transform[] objectsToSort ;
private void Foo()
{
Vector3 circleCenter = Vector3.zero ;
Vector3 circleNormal = Vector3.up ;
System.Array.Sort( objectsToSort, Sort( circleCenter, circleNormal ) );
for ( int i = 0 ; i < objectsToSort.Length ; ++i )
Debug.Log( objectsToSort*.name );*
}
private System.Comparison Sort( Vector3 circleCenter, Vector3 circleNormal )
{
return ( transform1, transform2 ) =>
{
float angle = SignedAngle( transform1.position - circleCenter, transform2.position - circleCenter, circleNormal ) ;
if ( Mathf.Approximately( angle, 0 ) ) return 0;
return (int) Mathf.Sign( angle );
};
}
public static float SignedAngle( Vector3 from, Vector3 to, Vector3 normal )
{
// angle in [0,180]
float angle = Vector3.Angle( from, to );
float sign = Mathf.Sign( Vector3.Dot( normal, Vector3.Cross( from, to ) ) );
return angle * sign;
}