void Update() { //Player within detection radius float dist = (target.transform.position - transform.position).magnitude; if (dist <= detectionRadius) { /*if (engageOnlyIfVisible) * { * RaycastHit2D hit = Physics2D.Raycast(transform.position, target.transform.position, detectionRadius); * * //First object hit is not our target * if (hit.collider.gameObject != target.gameObject) * return; * }*/ if (!playerDetected) { playerDetected = true; if (OnPlayerDetected != null) { OnPlayerDetected(); } } //Player within attack radius if ((dist <= attackRadius) && currentAttackCD <= 0) { currentAttackCD = pawn.cooldown; pawn.LookAt(target.transform.position); pawn.MeleeAttack(); pawnAgent.Stop(); pursuing = false; } //Adjust path only if we can attack if (currentReadjustCD <= 0 && currentAttackCD <= 0) { pursuing = true; Vector3 target = GetNearestAdyacent(); pawnAgent.MoveTowards(target); currentReadjustCD = pathReadjustCooldown; } } else if (playerDetected) { playerDetected = false; pursuing = false; pawnAgent.Stop(); if (OnPlayerUnDetected != null) { OnPlayerUnDetected(); } } //Attack cooldown if (currentAttackCD > 0) { currentAttackCD -= Time.deltaTime; } //Readjustment cooldown if (currentReadjustCD > 0) { currentReadjustCD -= Time.deltaTime; } }