-1

I have some problems with a script for a "Field Of View" of the enemy. After watching a tutorial to create a field of view for the player I thought I can switch it to the enemy's so that they detect the player and do some other stuff. I created a boolean variable playerInRange to detect if the enemies can detect the player and set this variable to true or false.

It works fine with just one enemy. When I add another one, the new enemy will not detect the player. So maybe it is related to the coroutine, but I am not sure.

Here is a little bit of my code:

void Start() {
    StartCoroutine("FindTargetsWithDelay", .2 f);
}

IEnumerator FindTargetsWithDelay(float delay) {
    while (true) {
        yield
        return new WaitForSeconds(delay);
        FindVisibleTargets();
    }
}

public void FindVisibleTargets() {
    visibleTargets.Clear();

    Collider[] targetsInViewRadius = Physics.OverlapSphere(transform.position, viewRadius, targetMask);

    for (int i = 0; i < targetsInViewRadius.Length; i++) {
        Transform target = targetsInViewRadius[i].transform;
        Vector3 dirToTarget = (target.position - transform.position).normalized;
        if (Vector3.Angle(transform.forward, dirToTarget) < viewAngle / 2) {
            float dstToTarget = Vector3.Distance(transform.position, target.position);

            if (!Physics.Raycast(transform.position, dirToTarget, dstToTarget, obstacleMask)) {

                // Not so nice solution!
                // The movement should be in a separate script!
                visibleTargets.Add(target);
                nav.SetDestination(player.position);
                anim.SetBool("IsRunning", true);

                if (dstToTarget < attackRange) {
                    playerInRange = true;
                    Debug.Log(playerInRange);
                }

            }
        } else {
            anim.SetBool("IsRunning", false);
            playerInRange = false;
            Debug.Log(playerInRange);
        }
    }
}
TrebledJ
  • 8,713
  • 7
  • 26
  • 48
t3chnico
  • 64
  • 6
  • OverlapSphere has a targetMask, is this variable set correctly the player's layer? – Rob Aug 19 '16 at 10:23
  • Hi Rob, thank you for your comment. The targetMask is setted correctly (for my point of view): **public LayerMask targetMask;** I can choose which layer is the target layer. The first enemy is working fine with it but not the second. :/ – t3chnico Aug 19 '16 at 10:47
  • 2
    You don't say HOW you "added another one". I suspect you didn't read what this script is doing hint: if first enemy sees player, but second does not, what happens? ... what about if first does NOT see player, but second does?) – Adam Aug 19 '16 at 12:36
  • @t3chnico could you update your question with a couple of screenshots of the objects in the hierarchy during play. – Rob Aug 19 '16 at 15:46
  • Just checking - did you attach the script to all of your other enemies (and/or your enemy prefab) for sure? – Ethan The Brave Aug 19 '16 at 17:45

1 Answers1

-1

Thank you guys for your little hint. It was really a little hierarchy problem :( Sorry for that newbie/DAU question. Cheers Nico

t3chnico
  • 64
  • 6