void AttackTarget() { if (currentTarget == null) { currentTarget = FindClosestTarget(); } if (currentTarget != null && currentTarget.alive) { wanderPoint = currentTarget.transform.position; if (gameManager.LineOfSight(transform, currentTarget.transform.position, gun.firePoint.position, enemyStats.lookRange, enemyStats.fov, obstacleMask)) { attacking = true; anim.SetBool("shooting", true); StopPatrol(); transform.rotation = gameManager.TurnToFace(transform, currentTarget.transform.position, 5f, true); handSlot.rotation = gameManager.TurnToFace(handSlot, currentTarget.chest.position, 5f, false); if (CheckWaitTimer(Random.Range((enemyStats.attackRate / 2), (enemyStats.attackRate * 2)))) { timeElapsed = 0; gun.Shoot(this); } } else { agent.destination = currentTarget.transform.position; //attacking = false; anim.SetBool("shooting", false); } } else { anim.SetBool("shooting", false); attacking = false; currentTarget = null; } }
private void Update() { // Regenerate AI's Health. if (health < 100) { health += healthRegen; } if (AIGlobals.Instance.WaveManager.aiWaveInProgress == false && preparingForWave == false) { StartCoroutine(PrepareForWave()); } // If there is no target and agent is unable to move, allow him to move. if (target == null && agent.isStopped) { agent.isStopped = false; } if (aiGun.isReloading && agent.speed != 1.8f) { agent.speed = 1.8f; } else if (aiGun.isReloading == false && agent.speed != 3.5f) { agent.speed = 3.5f; } // Get a target (closest enemy). target = GetClosestEnemy(); // If we don't have a target, we wander for enemies. if (target == null && agent.velocity.magnitude <= 0.001f) { // If we have pickupables, go to them. if (GetClosestPickupable() != null) { agent.SetDestination(GetClosestPickupable().position); return; } // Get current AI's ground renderer. Renderer groundRenderer = (Exchange.instance.arePlacesSwitched ? PlayerGlobals.Instance : AIGlobals.Instance).Ground.GetComponent <Renderer>(); NavMeshHit hit; NavMesh.SamplePosition(new Vector3(transform.position.x + Random.Range(-groundRenderer.bounds.extents.x, groundRenderer.bounds.extents.x), 1, transform.position.z + Random.Range(-groundRenderer.bounds.extents.z, groundRenderer.bounds.extents.z)), out hit, 2.0f, NavMesh.AllAreas); // Get a position to wander. Vector3 wanderPosition = hit.position; // = new Vector3(transform.position.x + Random.Range(-groundRenderer.bounds.extents.x, groundRenderer.bounds.extents.x), 1, transform.position.z + Random.Range(-groundRenderer.bounds.extents.z, groundRenderer.bounds.extents.z)); // Go to the wander position. agent.SetDestination(wanderPosition); return; } // If we don't have a target and we won't wander, we shouldn't do anything. if (target == null) { return; } float distance = Vector3.Distance(agent.transform.position, target.transform.position); // If we are close to our target, we need to start shooting. if (distance < 8.0f) { // Stop te agent. agent.isStopped = true; // Look at the target. transform.LookAt(target); // Start shooting. aiGun.Shoot(); } else if (agent.velocity.magnitude <= 0.001f || agent.isOnNavMesh == false) { agent.isStopped = false; agent.SetDestination(target.position); } }