I have been dabbling in C# Scripts for a couple of years now. I have many small scripts which achieve many things, from moving Cameras and Objects independently, to creating arrays for many objects to represent Units and Soldiers. I will be posting some sample Scripts on here to give an indication of the knowledge I have accumulated so far.
You Can see some of my scripts on my Twitter Page….Click on the link below:
Code to order change multiple object formations
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeFormation : MonoBehaviour
{
public Transform commander;
public Transform currentSelection;
public Vector3 currentCommanderPosition;
public List<Transform> Soldiers = new List<Transform>();
private SelectAnObject selectAnObject;
private float zc;
private float xcols;
private float zcols;
private float xsq;
private float zsq;
private float soldierCount;
private void Awake()
{
selectAnObject = GetComponent<SelectAnObject>();
}
private void FixedUpdate()
{
commander = selectAnObject.objSelected;
currentSelection = selectAnObject.currentCommanderSelected;
}
private void Update()
{
if (currentSelection != null)
{
if (currentSelection.transform.CompareTag("Commander"))
{
if (Input.GetKey(KeyCode.C))
{
SingleColumn();
}
if (Input.GetKey(KeyCode.Z))
{
Columns();
}
if (Input.GetKey(KeyCode.Q))
{
MakeSquare();
}
}
}
else currentSelection = null;
}
private void SingleColumn()
{
zc = -1.0f;
foreach (var item in Soldiers)
{
item.localPosition = Vector3.MoveTowards(item.localPosition, currentCommanderPosition, Time.maximumDeltaTime);
zc = zc - 1.5f;
currentCommanderPosition = new Vector3(0, 0, zc);
}
}
private void Columns()
{
xcols = 1.0f;
zcols = -1.0f;
for (int i = 0; i < 12; i++)
{
Soldiers[i].localPosition = Vector3.MoveTowards(Soldiers[i].localPosition, currentCommanderPosition, Time.deltaTime * 8);
zcols = zcols - 2.0f;
currentCommanderPosition = new Vector3(xcols, 0, zcols);
}
zcols = -1.0f;
for (int i = 12; i < 24; i++)
{
xcols = 5.0f;
Soldiers[i].localPosition = Vector3.MoveTowards(Soldiers[i].localPosition, currentCommanderPosition, Time.deltaTime * 8);
zcols = zcols - 2.0f;
currentCommanderPosition = new Vector3(xcols, 0, zcols);
}
zcols = -1.0f;
for (int i = 24; i < 37; i++)
{
xcols = 9.0f;
Soldiers[i].localPosition = Vector3.MoveTowards(Soldiers[i].localPosition, currentCommanderPosition, Time.deltaTime * 8);
zcols = zcols - 2.0f;
currentCommanderPosition = new Vector3(xcols, 0, zcols);
}
}
private void MakeSquare()
{
xsq = 1.0f;
zsq = 0;
soldierCount = Soldiers.Count / 4;
for (int i = 0; i < soldierCount; i++)
{
Soldiers[i].localPosition = Vector3.MoveTowards(Soldiers[i].localPosition, currentCommanderPosition, Time.deltaTime * 8);
xsq = xsq + 2.0f;
currentCommanderPosition = new Vector3(xsq, 0, zsq);
}
}
private void Square()
{
xsq = 1.0f;
zsq = 0;
for (int i = 0; i < 6; i++)
{
Soldiers[i].localPosition = Vector3.MoveTowards(Soldiers[i].localPosition, currentCommanderPosition, Time.deltaTime * 8);
xsq = xsq + 2.0f;
currentCommanderPosition = new Vector3(xsq, 0, zsq);
}
xsq = 1.0f;
for (int i = 6; i < 12; i++)
{
zsq = -2.0f;
Soldiers[i].localPosition = Vector3.MoveTowards(Soldiers[i].localPosition, currentCommanderPosition, Time.deltaTime * 8);
xsq = xsq + 2.0f;
currentCommanderPosition = new Vector3(xsq, 0, zsq);
}
xsq = 1.0f;
for (int i = 12; i < 18; i++)
{
zsq = -4.0f;
Soldiers[i].localPosition = Vector3.MoveTowards(Soldiers[i].localPosition, currentCommanderPosition, Time.deltaTime * 8);
xsq = xsq + 2.0f;
currentCommanderPosition = new Vector3(xsq, 0, zsq);
}
xsq = 1.0f;
for (int i = 18; i < 24; i++)
{
zsq = -6.0f;
Soldiers[i].localPosition = Vector3.MoveTowards(Soldiers[i].localPosition, currentCommanderPosition, Time.deltaTime * 8);
xsq = xsq + 2.0f;
currentCommanderPosition = new Vector3(xsq, 0, zsq);
}
xsq = 1.0f;
for (int i = 24; i < 30; i++)
{
zsq = -8.0f;
Soldiers[i].localPosition = Vector3.MoveTowards(Soldiers[i].localPosition, currentCommanderPosition, Time.deltaTime * 8);
xsq = xsq + 2.0f;
currentCommanderPosition = new Vector3(xsq, 0, zsq);
}
xsq = 1.0f;
for (int i = 30; i < 37; i++)
{
zsq = -10.0f;
Soldiers[i].localPosition = Vector3.MoveTowards(Soldiers[i].localPosition, currentCommanderPosition, Time.deltaTime * 8);
xsq = xsq + 2.0f;
currentCommanderPosition = new Vector3(xsq, 0, zsq);
}
}
}