Unity Auto detecting...

I have this code I made for detecting enemies based on the distance from the enemy to the player and then updating the detection level in an over all game script which then changes the a GUI texture to show the player that they are getting closer or farther away from an enemy... so it works perfect except one major thing: Every enemy is detecting the distance from them to the player and apparently if you do not kill the enemy that is closest to you it will not detect any other enemy... Im not telling the script to do this at all... granted it would be nice to know which one is closest and go off that one for the detection level but how can I get this to update to the next enemy if you do not kill the one before it? It seems as though Unity is determining which one is closest since Im not doing that anywhere in code...

thanks! Code below:

Detection system on Enemy Ai

detectDistance = Vector3.Distance (player2.transform.position, transform.position);

    if (Game_script.playerStealthed == false)
    {

        if (detectDistance <= 20)
        {
            detectionLevel = 2;
            Game_script.enemyDetectionLevel = 2;
        }
        else if (detectDistance > 20)
        {
            detectionLevel = 1;
            Game_script.enemyDetectionLevel = 1;
        }

        if(detectDistance <= 15)
        {
            detectionLevel = 3;
            Game_script.enemyDetectionLevel = 3;
        }

        if(detectDistance<= 10)
        {
            detectionLevel = 4;
            Game_script.enemyDetectionLevel = 4;
        }

        if(detectDistance <= 5)
        {
            detectionLevel = 5;
            Game_script.enemyDetectionLevel = 5;
            enemychase();
        }   

        if (enemyState == "attackmode")
        {       
            if(detectDistance >= 15)
            {
                detectionLevel = 3;
                Game_script.enemyDetectionLevel = 3;
                enemyState = "looking";
            }
        }
    }


Game_script GUI change based on detection lvl of Ai

if(controlerChosen)
    {
        if(stealthedToggler <= 0)
        {
            gStealth.guiTexture.texture = stealthed;
        }
        else if (stealthedToggler > 0)
        {
            gStealth.guiTexture.texture = notStealthed;
        }

        if(!playerStealthed)
        {

            if (enemyDetectionLevel >= 1)
            {
                gGauge.guiTexture.texture = detctLvl1;
            }

            if (enemyDetectionLevel >= 2)
            {
                gGauge.guiTexture.texture = detctLvl2;
            }

            if (enemyDetectionLevel >= 3)
            {
                gGauge.guiTexture.texture = detctLvl3;
            }

            if (enemyDetectionLevel >= 4)
            {
                gGauge.guiTexture.texture = detctLvl4;
            }

            if (enemyDetectionLevel >= 5)
            {
                gGauge.guiTexture.texture = detctLvl5;
            }
        }
        else if (playerStealthed)
        {
            if (!iSeeYou)
            {
                enemyDetectionLevel = 1;
                gGauge.guiTexture.texture = detctLvl1;
            }
            else
            {
                enemyDetectionLevel = 5;
                gGauge.guiTexture.texture = detctLvl5;
            }
        }
    }

Well, it's really hard to read that code. You mixed different programmingstyles and your indention seems a bit "random". Like owenpat already mentioned there are some really strange "constructs" that doesn't make much sense.

You used a lot of static variables from your `Game_script` and i think that's the main problem. Every enemy that have a greater distance than 20 will set your static var `enemyDetectionLevel`. All do that in the same frame.

What is the `detectionLevel` var even good for, because it gets written but never read? And why is it a float?

function enemyAttack is a coroutine and if your state is "attacking" it gets called every frame but you have two yield in there so there are 3 functions running at the same time... I don't know if that was intended but it looks strange to me.


edit

Well, since you added that you want to display the closest enemy’s detection level, we can go on from there.

The easiest way to find the closest enemy would be to use Physics.OverlapSphere in your playerscript to get all colliders that are in range (eg. 20). Just sort them out and test for enemies only. If you have tagged your enemies you can use the tag or even simpler and faster to place them on a seperate layer and restrict OverlapSphere to check this layer only.

You will end up with some thing like that:

// Enemy are tagged with "Enemy" the AI script is called "EnemyAI"
var allNearbyColliders = Physics.OverlapSphere(transform.position, 20);
var closestEnemy : EnemyAI = null;
for(var current : Collider in allNearbyColliders) {
    if (current.tag == "Enemy"){
        if (closestEnemy == null)
             closestEnemy = current.GetComponent.<EnemyAI>();
        else {
            var currentEnemy : EnemyAI = current.GetComponent.>EnemyAI>();
            if (currentEnemy.detectDistance < closestEnemy.detectDistance)
                closestEnemy = currentEnemy;
        }
    }
}

// ********************************************

// Or with all enemies on a special layer:
// Enemy are on layer 10 and the AI script is called "EnemyAI"
var allNearbyEnemies = Physics.OverlapSphere(transform.position, 20, 1<<10);
var closestEnemy : EnemyAI = null;
for(var current : Collider in allNearbyEnemies) {
    if (closestEnemy == null)
        closestEnemy = current.GetComponent.<EnemyAI>();
    else {
        var currentEnemy : EnemyAI = current.GetComponent.<EnemyAI>();
        if (currentEnemy.detectDistance < closestEnemy.detectDistance)
            closestEnemy = currentEnemy;
    }
}

After that loop you will have the closest enemy AI-script reference in the `closestEnemy` variable. If no enemy is in range, `closestEnemy` will be null, so keep that in mind! Now you can just use the detectionLevel variable of that enemy.

if (closestEnemy != null)
    enemyDetectionLevel = closestEnemy.detectionLevel;

Note: This requires the variables `detectDistance` and `detectionLevel` to be public (in JS simply not private ;) ) and your AI script is named EnemyAI. If your script have a different name, you have to adjust it accordingly (either rename the script or change it in my sample code to match your name).

If you want to know how to setup you layer just look at the documentation on layers.

All this is untested but it should work. Maybe if i have some time i could try it myself.

Good luck! again :wink:

After hours of altering Bunny83's code i finally got it working perfectly - but thank you Bunny! That gave me a greate base to work from :D below is the final code:

var allNearByColliders = Physics.OverlapSphere(transform.position, 20);
    var currentEnemy : enemymove;
    for (var current : Collider in allNearByColliders)
    {
        if (current.tag == "Enemy")
        {
            if (currentEnemy == null)
            {
                currentEnemy = current.GetComponent.<enemymove>();
            }

            if (currentEnemy)
            {
                var closestEnemy : enemymove = current.GetComponent.<enemymove>();
                if (closestEnemy.detectDistance <= currentEnemy.detectDistance)
                {
                    distanceToEnemy = Vector3.Distance (closestEnemy.transform.position, transform.position);
                    Debug.DrawLine (transform.position, closestEnemy.transform.position, Color.red);//shows how many enemies are in range and where they are and how close to player

                    if (Game_script.playerStealthed == false)
                    {

                        if (distanceToEnemy <= 20)
                        {
                            Game_script.enemyDetectionLevel = 2;
                        }
                        else if (distanceToEnemy >= 20)
                        {
                            Game_script.enemyDetectionLevel = 1;
                            closestEnemy = null;
                        }

                        if(distanceToEnemy <= 15)
                        {
                            Game_script.enemyDetectionLevel = 3;
                        }

                        if(distanceToEnemy <= 10)
                        {
                            Game_script.enemyDetectionLevel = 4;
                        }

                        if(distanceToEnemy <= 5)
                        {
                            Game_script.enemyDetectionLevel = 5;
                        }
                    }
                }
            }
        }
    }