I am making a turn based game with multiple units and I just finished my movement system but only for one unit. However, I need to make it work for multiple units and am wondering if there is a way to do so with the use of tags. For ease of reading, rifleman refers to the image and Infantry refers to the sprite. Any information you could give about Tags can help. THANK YOU!
public void Update() {
cursorPosition = cursor.transform.position;
entityPosition = transform.position;
float dist = Vector3.Distance(entityPosition, cursorPosition);
if ((dist == 0) && Input.GetButtonDown("Select"))
{
toggleMove = true;
if (baseCpt || cityCpt || airportCpt) {
} else {
GameObject.Find ("Capture").GetComponent<Image> ().enabled = false;
GameObject.Find ("Capture").GetComponent<Button> ().enabled = false;
GameObject.Find ("Capture").GetComponentInChildren<Text> ().enabled = false;
}
}
while (toggleMove == true) {
GameObject.Find ("cursor").GetComponent<CursorMove> ().enabled = true;
GameObject.Find ("Canvas").GetComponent<Canvas> ().enabled = false;
while (Input.GetButtonDown ("Select") && GameObject.Find ("B-Copter").transform.position != GameObject.Find ("cursor").transform.position) {
GameObject.Find ("B-Copter").transform.position = GameObject.Find ("cursor").transform.position;
GameObject.Find ("Canvas").GetComponent<Canvas> ().enabled = true;
GameObject.Find ("cursor").GetComponent<CursorMove> ().enabled = false;
toggleMove = false;
break;
}
break;
}
}
It is bad practice to use Tags in Unity this way.
Tags are implemented so you can provide some information about a GameObject without having to use GetComponent over all your types. E.g. if you’ve tagged it as LocalPlayer or Player then, in code, you can check against this and use GetComponent<PlayerController>() to get that player’s PlayerController component - guaranteeing that it wont return null basically.
If you want to create a turn based scenario that allows for legitimate turn based control over individual Controllers, then you should create a GameController or TurnController which manages turns for you.
Here’s some example code of what I would do:
public abstract class TurnBasedObject : MonoBehaviour {
// Shorthand for GetInstanceID()
public int ID {
get {
return GetInstanceID();
}
}
public bool IsTurn {
get {
return TurnController.Instance.GetTurn(this);
}
}
public void EndTurn() {
TurnController.Instance.EndTurn(this);
}
}
// The following two objects are examples of what you would make out of this.
// e.g. You can have multiple different types of players (assymetrical gameplay)
// Or you could only have just the Tank type or just the Soldier type.
// The gameplay will STILL BE TURNBASED and every instance of these objects
// Will make one move per turn (unless you have a different setup for turn based gameplay).
public class Tank : TurnBasedObject {
void Update() {
if (IsTurn) {
// Do stuff
if (ReadyToEndTurn) EndTurn();
}
}
}
public class Soldier : TurnBasedObject {
void Update() {
if (IsTurn) {
// Do stuff
if (ReadyToEndTurn) EndTurn();
}
}
}
public class TurnController : MonoBehaviour {
public List<TurnBasedObject> TurnObjects {get; private set; }
public int CurrentTurn;
public static TurnController Instance { get; private set; }
void Awake() {
// Typical singleton construction
if (Instance != null) {
Instance = this;
return;
}
TurnObjects = new List<TurnBasedObject>();
}
void Start() {
TurnBasedObject[] tbObjects = GameObject.FindObjectsOfType<TurnBasedObject>();
}
public bool GetTurn(TurnBasedObject tbo) {
return CurrentTurn == TurnObjects.FindIndex(obj => obj == tbo);
}
public void EndTurn(TurnBasedObject tbo) {
if (GetTurn(tbo)) {
// Set next turn
CurrentTurn++;
if (CurrentTurn >= TurnObjects.Length) CurrentTurn = 0;
}
else {
// Uh oh, someone tried ending their turn when it wasn't their turn
// Do something here about that if you want
}
}
}