Hi guy I have been stuck in this problem for more than 10 days

Hello, my friends. I hope someone can help me with this specific script I have. I’m working on programming a strategy game, and now I’m facing an issue with one of the scripts. It consumes high resources and creates a frame drop for me. The issue is that when the player searches for an object within the range, it executes the condition.

Please note that the translation is provided as is and may not perfectly reflect the intended meaning.

And now I’m going to write for you the line of code that is consuming high resources for me. I hope you can find a solution for me. I have great respect, appreciation, and love for you.

private void Update()
{
Collider enemies = Physics.OverlapSphere(transform.position, detectionRange,
LayerMask.GetMask(enemyTag));

GameObject nearestEnemy = GetNearestEnemy(enemies);
}

private GameObject GetNearestEnemy(Collider enemies)
{

    GameObject nearestEnemy = null;
    float nearestDistance = Mathf.Infinity;
    foreach (Collider enemy in enemies)
    {
        float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position);
        if (distanceToEnemy < nearestDistance)
        {
            nearestDistance = distanceToEnemy;
            nearestEnemy = enemy.gameObject;
            Target = nearestEnemy;
        }

    }
    return nearestEnemy;
}

How many enemies are you trying to iterate over every frame? I am not sure if you need to be checking for enemies every frame and instead can pulse the detection in some frequency. Perhaps when you have the nearest distance enemy you can stop detecting enemies until some new condition takes place - break out of your loop when an enemy is within some threshold distance. From the information you provided I can only think of limiting the amount of times you are collecting the enemy colliders and iterating over each one.

Approximately, the number of enemies is around 25.

There are a few things you can do to improve the performance here, but I don’t know if it will improve it by much.

First, cache the layer mask. You don’t have to create it every time.

Next, perhaps use Physics.OverlapSphereNonAlloc. The documentation is here

Then, since you are only looking for the nearest enemy and aren’t interested in the exact distance, you could skip half of the distance calculation. Instead of Vector3.Distance(), just get the square magnitude of the vector between here and the enemy.

float sqrMagnitude = (enemy.transform.position - transform.position).sqrMagnitude;

It skips the square-root calculation, which computers are pretty bad at, so the value is much larger than the actual distance, but you will still find the nearest by checking for the smallest value.

Other notes: You are setting a Target value every time you find a closer enemy. Perhaps don’t do this. Ideally, this Target should be set to the result of this method but if you insist on doing it in here, put it at the end of the foreach loop.

Lastly, like @Ironlionm4n said; maybe you don’t need to check for the nearest enemy on every frame. Even if you don’t currently have a ‘Target’, you still don’t have to check every frame. Unless the detection range is really small. You could probably get away with only doing this check every 10 frames or 10 times per second (every 100 milliseconds).

2 Likes

thank you very very much :heart::heart:

ok i will try thanks bro

Privacy & Terms