- Home /
This post has been wikified, any user with enough reputation can edit it.
Attack to enemy soldier
Hi. I have very hard problem(for me). I making a strategy game like Europa Universalis. I have selected a nation (example Britons). When game start, Britons at war with France. Now my war system is:
//its English Gameobject
public GameObject AtWar[];
I attach France Gameobject to English gameobject's AtWar var.
Now Its My Problem: soldier AI:
public float distance;
GameObject nearest;
GameObject FindClosestEnemy() {
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag("AtWarEmpire");
GameObject closest=null;
float distance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (GameObject go in gos) {
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closest = go;
distance = curDistance;
}
}
nearest=closest;
return closest;
}
void Update(){
distance=Vector3.Distance(gameObject.transform.position,nearest.transform.position);
if (distance<10){
transform.position = Vector3.MoveTowards(transform.position, yakin2.position, 3*Time.deltaTime);
}
}
I wanna make this:
Soldier find nearest AT WAR empire's soldier.
how to i can make this?
Sorry for bad English
Comment