// Update is called once per frame public virtual void Update(Unit unit) { //If unit has arrived at destination then stop it float distance = Vector3.Distance(unit.getDestination(), unit.transform.position); if (distance < stoppingDistance) { unit.GetComponent<NavMeshAgent>().destination = unit.transform.position; unit.setDestination (unit.transform.position); unit.changeState (new Idle()); } if (unit.GetComponent<NavMeshAgent>().nextPosition == unit.getDestination ()) { unit.GetComponent<NavMeshAgent>().updatePosition = false; } /* COMBAT (DRIVEBY STYLE) */ foreach (Collider enemy in Physics.OverlapSphere (unit.transform.position, unit.getRange(),1<<10)){ Unit enemyUnit = enemy.GetComponent<Unit>(); if (enemyUnit != null && unit.team != enemyUnit.team){ // cant we all just get along? unit.lookAtPos(enemyUnit.transform.position); if(unit.canFire ()){ enemyUnit.GetComponent<Health>().applyDamage(unit.getDamage()); unit.resetCooldown (); unit.drawAttack(unit,enemyUnit, unit.renderer.material.color); } break; } } }
// Update is called once per frame public virtual void Update(Unit unit) { // if target has died if (unit.getTarget() == null || unit.getTarget().GetComponent<Health>().currentHealth <= 0){ unit.setTarget(null); unit.setDestination(Vector3.zero); unit.GetComponent<NavMeshAgent>().updatePosition = false; unit.changeState (new Idle()); return; } // else, if enemy still alive Unit lastEnemy = null; bool inRange = false; foreach (Collider enemy in Physics.OverlapSphere (unit.transform.position, unit.getRange(),1<<10)){ Unit enemyUnit = enemy.GetComponent<Unit>(); if (enemyUnit != null && unit.team != enemyUnit.team){ lastEnemy = enemyUnit; if (enemyUnit == unit.getTarget()){ inRange = true; unit.setDestination (Vector3.zero); unit.GetComponent<NavMeshAgent>().updatePosition = false; // attack target unit unit.lookAtPos(enemyUnit.transform.position); if(unit.canFire ()){ enemyUnit.GetComponent<Health>().applyDamage(unit.getDamage()); unit.resetCooldown (); unit.drawAttack(unit,enemyUnit, unit.renderer.material.color); } break; } } } if (!inRange){ // for now, it finds a path every update. this will be fixed in future unit.GetComponent<NavMeshAgent>().destination = unit.getTarget ().transform.position; unit.setDestination(unit.getTarget ().transform.position); unit.GetComponent<NavMeshAgent>().updatePosition = true; if (lastEnemy != null){ // not in range of target, but in range of another enemy unit.lookAtPos(lastEnemy.transform.position); if(unit.canFire ()){ lastEnemy.GetComponent<Health>().applyDamage(unit.getDamage()); unit.resetCooldown (); unit.drawAttack(unit,lastEnemy, unit.renderer.material.color); } } } }
// Update is called once per frame public virtual void Update(Unit unit) { /* COMBAT (HOLD POSITION STYLE) */ foreach (Collider enemy in Physics.OverlapSphere (unit.transform.position, unit.getRange(),1<<10)){ Unit enemyUnit = enemy.GetComponent<Unit>(); if (enemyUnit != null && unit.team != enemyUnit.team){ // cant we all just get along? unit.lookAtPos(enemyUnit.transform.position); if(unit.canFire ()){ enemyUnit.GetComponent<Health>().applyDamage(unit.getDamage()); unit.resetCooldown (); unit.drawAttack(unit,enemyUnit, unit.renderer.material.color); } break; } } }