Onto lesson 2
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
public class UnitMover : MonoBehaviour
|
||||
{
|
||||
@@ -13,4 +14,41 @@ public class UnitMover : MonoBehaviour
|
||||
{
|
||||
|
||||
}
|
||||
//calculates a unit formation around a given location
|
||||
public static Vector3[] GetUnitGroupDestination(Vector3 moveToPos, int numUnits, float unitGap)
|
||||
{
|
||||
//vector3 array for final destinations
|
||||
Vector3[] destinations = new Vector3[numUnits];
|
||||
//calculate the rows and columns
|
||||
int rows = Mathf.RoundToInt(Mathf.Sqrt(numUnits));
|
||||
int cols = Mathf.CeilToInt((float)numUnits / rows);
|
||||
//we need to know the current row and column we're calculating
|
||||
int curRow = 0;
|
||||
int curCol = 0;
|
||||
float width = ((float)rows - 1) * unitGap;
|
||||
float length = ((float)cols - 1) * unitGap;
|
||||
for(int x = 0; x < numUnits; x++)
|
||||
{
|
||||
destinations[x] = moveToPos = (new Vector3(curRow, 0, curCol) * unitGap) - new Vector3(length / 2, 0, width / 2);
|
||||
curCol++;
|
||||
if(curCol == rows)
|
||||
{
|
||||
curCol = 0;
|
||||
curRow++;
|
||||
}
|
||||
}
|
||||
return destinations;
|
||||
}
|
||||
public static Vector3[] GetUnitGroupDestinationsAroundResource(Vector3 resourcePos, int unitsNum)
|
||||
{
|
||||
Vector3[] destinations = new Vector3[unitsNum];
|
||||
float unitDistanceGap = 160.0f / (float)unitsNum;
|
||||
for(int x = 0; x < unitsNum; x++)
|
||||
{
|
||||
float angle = unitDistanceGap * x;
|
||||
Vector3 dir = new Vector3(Mathf.Sin(angle * Mathf.Deg2Rad), 0, Mathf.Cos(angle * Mathf.Deg2Rad));
|
||||
destinations[x] = resourcePos + dir;
|
||||
}
|
||||
return destinations;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user