private void MoveToUnit(UnitController1 unit) { int xDis, zDis, moves; xDis = (int)Mathf.Abs(unit.transform.position.x - transform.position.x); zDis = (int)Mathf.Abs(unit.transform.position.z - transform.position.z); if (xDis >= zDis) { if (xDis > 0) { moves = 1; } else { moves = -1; } transform.position = new Vector3((transform.position.x + moves), transform.position.y, transform.position.z); } else { if (zDis > 0) { moves = 1; } else { moves = -1; } transform.position = new Vector3(transform.position.x, transform.position.y, (transform.position.z + moves)); } }
//public int Width //{ // get { return width; } //} //public int Length //{ // get { return length; } //} // Start is called before the first frame update void Start() { Width = width; Length = length; transform.localScale = new Vector3(width, 1, length); transform.position = new Vector3(width / 2, 0, length / 2); UnitController1 unitController = MapSpawner.units[0].GetComponent <UnitController1>(); }
void Attack(UnitController1 target) { target.Health -= attack; //Debug.Log(target + " has"+Health+" health."); if (target.Health <= 0) { target.Dead(); //Debug.Log(target + " has died."); } }
void FindTarget() { GameObject[] units = GameObject.FindGameObjectsWithTag("Unit"); GameObject[] buildings = GameObject.FindGameObjectsWithTag("Building"); dis = float.MaxValue; targetedUnit = null; targetedBuilding = null; if (buildings != null) { for (int a = 0; a < buildings.Length; a++) { closestBuilding = buildings[a].GetComponent <BuildingController>(); if (closestBuilding == null || closestBuilding.Team == Team) { continue; } tempDis = (buildings[a].transform.position - transform.position).magnitude; if (tempDis < dis) { targetedBuilding = closestBuilding; dis = tempDis; } } } if (units != null) { for (int b = 0; b < units.Length; b++) { closestUnit = units[b].GetComponent <UnitController1>(); if (closestUnit == null || closestUnit.Team == Team) { continue; } tempDis = (units[b].transform.position - transform.position).magnitude; if (tempDis < dis) { targetedUnit = closestUnit; dis = tempDis; } } } }
private void InitializeUnits() { for (int i = 0; i < units.Length; i++) { int x = Random.Range(1, (int)map.transform.localScale.x); int z = Random.Range(1, (int)map.transform.localScale.z); int unitType = Random.Range(0, 5); //exclusive of the max value int faction = Random.Range(0, 2); // '' int hp = 0; if (unitType == 0) { units[i] = meleeUnitOr; hp = 20; } else if (unitType == 1) { units[i] = meleeUnitBl; hp = 20; } else if (unitType == 2) { units[i] = rangedUnitOr; hp = 15; } else if (unitType == 3) { units[i] = rangedUnitBl; hp = 15; } else { units[i] = wizardUnit; hp = 10; } units[i].transform.position = new Vector3(x, (float)0.5, z); Instantiate(units[i]);//puts units on the map (one at a time) UnitController1 uc = units[i].GetComponent <UnitController1>(); uc.hp = hp; } for (int j = 0; j < buildings.Length; j++) { int x = Random.Range(1, (int)map.transform.localScale.x); int z = Random.Range(1, (int)map.transform.localScale.z); int buildingType = Random.Range(0, 4); //exclusive of the max value int faction = Random.Range(0, 2); // '' int hp = 0; if (buildingType == 0) { buildings[j] = factoryBuildingOr; hp = 60; } else if (buildingType == 1) { buildings[j] = factoryBuildingBl; hp = 60; } else if (buildingType == 2) { buildings[j] = resourceBuildingOr; hp = 50; } else if (buildingType == 3) { buildings[j] = resourceBuildingBl; hp = 50; } buildings[j].transform.position = new Vector3(x, (float)0.5, z); Instantiate(buildings[j]); BuildingController bc = buildings[j].GetComponent <BuildingController>(); bc.hp = hp; } }