public override TaskStatus OnUpdate() { IA_AutoShootSystem aiShoot = targetGameobject.GetComponent <IA_AutoShootSystem>(); aiShoot.gameObject.GetComponent <NavMeshAgent>().isStopped = false; Vector3 ammoPoint; if (aiShoot.target != null) { ammoPoint = IA_GameManager.GetClosestElement(aiShoot.gameObject.transform.position, AI_AmmoSpawner.instance.GetSpawnedPositions(), aiShoot.target.GetComponent <IA_AutoShootSystem>()); } else { ammoPoint = AI_AmmoSpawner.instance.GetSpawnedPositions()[0]; } aiShoot.target = null; if (targetGameobject.GetComponent <Patrol_Movement>() != null) { targetGameobject.GetComponent <Patrol_Movement>().SetDestination(ammoPoint); } else { targetGameobject.GetComponent <IA_Wander_Movement>().SetDestination(ammoPoint); } aiShoot.currentState = TankState.PICKING_AMMO; Debug.Log("Moving to new ammo box"); return(TaskStatus.COMPLETED); }
private void OnDeath() { // Set the flag so that this function is only called once. m_Dead = true; // Move the instantiated explosion prefab to the tank's position and turn it on. m_ExplosionParticles.transform.position = transform.position; m_ExplosionParticles.gameObject.SetActive(true); // Play the particle system of the tank exploding. m_ExplosionParticles.Play(); // Play the tank explosion sound effect. m_ExplosionAudio.Play(); // Turn the tank off. gameObject.SetActive(false); if (IA_GameManager.instance.sceneEnemyTanks.Contains(gameObject)) { IA_GameManager.instance.sceneEnemyTanks.Remove(gameObject); } IA_EnemySpawner.instance.DeleteEnemy(gameObject); IA_AutoShootSystem playerAT = IA_GameManager.instance.playerTank.GetComponent <IA_AutoShootSystem>(); if (playerAT.GetTarget() == gameObject) { playerAT.SetTarget(null); } if (gameObject == IA_GameManager.instance.playerTank) { IA_GameManager.instance.FinishGame(); } else { IA_GameManager.instance.score += 100; IA_EnemySpawner.instance.SpawnEnemy(); } Destroy(gameObject, m_ExplosionParticles.main.duration); Destroy(m_ExplosionParticles.gameObject, m_ExplosionParticles.main.duration); }
public static Vector3 GetClosestElement(Vector3 point, Vector3[] array, IA_AutoShootSystem target) { Vector3 closest = array[0]; NavMeshAgent targetAgent = target.GetComponent <NavMeshAgent>(); float lastDist = 0; for (int i = 0; i < array.Length; i++) { float cDist = Vector3.Distance(point, array[i]); if (cDist < lastDist && targetAgent.pathEndPosition != array[i]) // Not the fastest way tbh { closest = array[i]; lastDist = cDist; } } return(closest); }