best way to find out is player within enemy range

Hi All!

I’m looking for best way to find out is player within enemy range attack or not for my 2d Space rescue arcade game (video below).

First, i tried to create script attached to every enemy and used coroutine to calculate distance to player NOT every frame.
It works fine, you may see it on video.

Then I started to figure out is there a better way mostly for rapid fire enemies like laser and plasma cannons

I found this post:

Quote from it here:

If you use the Physics system like you originally thought, you can avoid the distance computations every frame. Add a SphereCollider to your AI. Set the radius to minRange. You could add the collider by hand or automatically within the EnemyAI script. Tick Is Trigger to make it a trigger. Since the AI will presumably move, add a Rigidbody and tick Is Kinematic (unless the AI already has a rigidbody).

Then add OnTriggerEnter and OnTriggerExit methods to keep track of when the player enters the trigger area:

This is not real but close to it code:

Class InRange

Public bool IsInRange = false;
void OnTriggerEnter2d(Collider2d other) {
if (other.tag == “Player”) IsInRange = true;
}

void OnTriggerExit2d(Collider2d other) {
if (other.tag == “Player”) IsInRange = false;
}

I tried it this way:

  1. add child game object to main enemy game object inside enemy prefab

  2. Add a circle Collider 2d to child object
    Set the radius to 100
    Tick Is Trigger to make it a trigger.
    I have Rigidbody on main game object so i didn’t add one more to child,but I tick Is Kinematic

  3. add script like quoted above but with 2d triggers

  4. modified coroutine in main game object:

  • at start - assignment for child component variable pointer to script with triggers above
  • at coroutine itself - check InRange.IsInRange boolean instead of calculating distance.

Good thing: triggers work as expected. IsInRange getting true or false.

Bad thing: game become extremely slow (1 fps!)in scene where I tried these new enemies with triggers.

Why? What is wrong?
I will post real code here soon

I don’t think the low fsp you’re getting is the result of setting triggers, it’s probably something else like an error on every frame. I’d exclude that first.

As for the range check, i’d use ray cast, specifically in your case circle ray cast (or sphere if you’re using 3d physics)

Check the docs, you might want to use Physics2D.CircleCastAll to get a full list of targets depending on your need.

Do a deep profile to figure out exactly why your game is grinding to a halt. I doubt it is the triggers.

As for another option, you can calculate distance on a schedule, such as once a second. Doing so would be less than 2% of the CPU usage compared to calculating distance every frame (at 60 FPS), and your players will likely barely notice the difference.

Could you please recommend good link to deep profiling usage?

Do not use triggers. This shit is still a bug LOL

Here you go. And also.


If you only have 1 player, there’s nothing wrong with checking the Vector3.Distance to the player transform every frame (or every so often). Not only is that very simple but it’s plenty performant and appropriate for a game like this.