Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question (rts units) Rotate formations when moving

Discussion in 'Navigation' started by Mellenbull, Mar 28, 2023.

  1. Mellenbull

    Mellenbull

    Joined:
    May 25, 2020
    Posts:
    3
    Im making units that move in a formation to a position that you clicked but i have run into a problem i do not know how to fix.

    The unit formation is a square formation and the problem i have run into is that the formation always faces the same direction because i just change x, z.

    How would i go about changing the rotation of the formation to where the units are moving?

    Here is the formation code

    public void EvaluatePoints(Vector3 pos)
    {
    float offsetX = 0;
    float offsetZ = 0;

    int Counter = 0;
    int unitCount = unitSelections.unitsSelected.Count;

    for (int i = 0; i < unitCount; i++)
    {
    UnitAI AI = unitSelections.unitsSelected.GetComponent<UnitAI>();

    AI.destination = new Vector3(pos.x + offsetX, pos.y, pos.z + offsetZ);

    Counter++;
    offsetX += 2;
    if(Counter > Mathf.Sqrt(unitCount))
    {
    offsetX = 0;
    offsetZ += 2;
    Counter = 0;
    }
    }
    }