Files
TutorialRTS/Assets/Scripts/UnitMover.cs

55 lines
1.8 KiB
C#
Raw Normal View History

2026-03-19 11:57:23 +00:00
using UnityEngine;
2026-03-19 17:31:51 +00:00
using UnityEngine.UIElements;
2026-03-19 11:57:23 +00:00
public class UnitMover : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
}
2026-03-19 17:31:51 +00:00
//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;
}
2026-03-19 11:57:23 +00:00
}