//spawn the an unit instance of given the prefab private UnitAI SpawnUnit(GameObject prefab, Vector3 spawnPos, Quaternion rot, string name = "") { //instantiate the unit, assign the layer and name //~ GameObject unitObj=(GameObject)Instantiate(prefab, spawnPos, rot); GameObject unitObj = ObjectPoolManager.Spawn(prefab, spawnPos, rot); unitObj.layer = TDS.GetLayerAIUnit(); unitObj.name = name; //get the UnitAI instance and assign target UnitAI unitInstance = unitObj.GetComponent <UnitAI>(); unitInstance.target = GameControl.GetPlayer(); //if(anchorToPoint) unitInstance.SetAnchorPoint(transform, anchorRadius); //not in use atm //override the unit default hitpoint if overrideHitPoint is enabled if (overrideHitPoint) { unitInstance.OverrideHitPoint(spawnHP, overrideHPMode); } AddUnit(unitInstance); //track unit return(unitInstance); }
public override void Awake() { base.Awake(); //initiation thisObj.layer = TDS.GetLayerAIUnit(); hostileUnit = true; //setup the cooldown if (!randFirstAttackDelay) { currentCD = firstAttackDelay; } else { currentCD = Random.Range(0, firstAttackDelay); } //make sure the setting for range attack is correct if (enableRangeAttack) { if (shootPointList.Count == 0) { shootPointList.Add(thisT); //make sure there a valid shoot point } if (shootObject == null) { Debug.LogWarning("shoot object unassigned for range attack unit, attack disabled", thisT); enableRangeAttack = false; } } //if using navmeshagent agent = thisObj.GetComponent <NavMeshAgent>(); if (agent != null) { agent.stoppingDistance = brakeRange; agent.speed = moveSpeed; } evadeCD = evadeCooldown; stopCD = stopCooldown; }
//aim split, the split shootobject will try to aim at unit in range void SplitAim(Collider col) { //first all all target in range LayerMask mask = 1 << TDS.GetLayerAIUnit(); Collider[] cols = Physics.OverlapSphere(thisT.position, splitRange, mask); if (cols.Length == 0) { Debug.Log("no target in range"); return; } //make sure we dont aim at the collider we hit in th first place again List <Collider> colList = cols.ToList(); for (int i = 0; i < colList.Count; i++) { if (colList[i] == col) { colList.RemoveAt(i); i -= 1; } } //randomly reduce the possible target so we dont exceed the split count List <Transform> targetList = new List <Transform>(); for (int i = 0; i < Mathf.Min(colList.Count, splitCount); i++) { int rand = Random.Range(0, colList.Count); targetList.Add(colList[i].transform); colList.RemoveAt(rand); } //not aim at each target and fire another shootobject at them for (int i = 0; i < targetList.Count; i++) { Quaternion shootRot = Quaternion.LookRotation(targetList[i].position - thisT.position); FireSplitSO(shootRot); } }