public void ProcessMover() { if (unitControl.IsDead() || unitControl.IsStunned() || unitControl.IsAttacking()) { return; } if (unitControl.HasEnemy()) { Transform enemy = unitControl.GetCurrentEnemy().transform; float minDist = Mathf.Max((unitAttack.attackRange / 2), 2); if ((transform.position - enemy.position).sqrMagnitude > minDist) { MoveTowardsTarget(enemy.position); } else { animator.SetBool("isRunning", false); } } else { Vector3 goalPos = homePosition; goalPos.y = transform.position.y; if ((transform.position - goalPos).sqrMagnitude > 1) { MoveTowardsTarget(goalPos); } else { animator.SetBool("isRunning", false); } } }
public void ProcessAttack() { if (unitControl.IsDead() || unitControl.IsAttacking() || unitControl.IsWaiting()) { return; } if (cooldown > 0) { cooldown -= Time.deltaTime; } if (!unitControl.GetCurrentEnemy() || unitControl.GetCurrentEnemy().IsDead()) { unitControl.FindNewEnemy(true); } else { float distance = (unitControl.GetCurrentEnemy().transform.position - transform.position).sqrMagnitude; if (attackRange > distance) { if (cooldown < 0) { Attack(); } } } }
void Update() { if (IsDead()) { return; } if (currentEnemy && currentEnemy.IsDead()) { currentEnemy = null; } if (knockBackTimer > 0) { knockBackTimer -= Time.deltaTime; if (knockBackTimer < 0) { ragdollControl.disableRagDoll(); transform.position = torso.parent.position; torso.parent.localPosition = Vector3.zero; } } if (!isWaiting) { unitAttack.ProcessAttack(); unitMover.ProcessMover(); } }
UnitControl GetNearbyEnemy(UnitControl closest) { if (!closest) { return(null); } Collider[] nearbyObjects = Physics.OverlapSphere(closest.transform.position, 3); UnitControl nearbyEnemy = null; List <UnitControl> nearbyEnemies = new List <UnitControl>(); foreach (Collider thing in nearbyObjects) { nearbyEnemy = thing.GetComponent <UnitControl>(); if (nearbyEnemy && !nearbyEnemy.IsDead() && !nearbyEnemy.gameObject.tag.Equals(gameObject.tag)) { nearbyEnemies.Add(nearbyEnemy); } } if (nearbyEnemies.Count == 0) { return(null); } return(nearbyEnemies[Random.Range(0, nearbyEnemies.Count)]); }
void Explode() { Collider[] nearbyObjects = Physics.OverlapSphere(transform.position, blastRadius); foreach (Collider thing in nearbyObjects) { UnitControl enemyInRange = thing.GetComponent <UnitControl>(); if (enemyInRange && !enemyInRange.IsDead()) { if (Vector3.Distance(transform.position, enemyInRange.transform.position) < blastRadius * 0.25f) { enemyInRange.Die(true); } else { enemyInRange.Knockback(transform.position, damage); } } } GameObject effect = Instantiate(explosionEffect, transform.position, Quaternion.identity) as GameObject; effect.gameObject.AddComponent <ParticleKiller>(); Destroy(gameObject); }
UnitControl GetClosestEnemy(GameObject[] enemies) { UnitControl closest = null; float minDist = Mathf.Infinity; Vector3 currentPos = transform.position; foreach (GameObject enemy in enemies) { UnitControl enemyUnit = enemy.GetComponent <UnitControl>(); float dist = Vector3.Distance(enemy.transform.position, currentPos); if (!enemyUnit) { Debug.Log(enemy.name + " has no unit control"); } if (dist < minDist && !enemyUnit.IsDead()) { closest = enemy.GetComponent <UnitControl>(); minDist = dist; } } return(closest); }