How do I make an enemy with a 'collision detector'?

Essentially, I want it to do something once the player is within a certain radius of it.

I planned to use an Invisible sprite that is larger than the enemy; then, when the player touches it, the enemy does something. However, the problem with that was that it wouldn’t affect the enemy that is on top of the larger sprite; instead, it would perform that action on a random enemy of the same type.

1 Like

I believe there is an option to do something similar in the sprite utilities extension.

2 Likes

The reason it’s performing on a random enemy is most likely because you are using the variable that you used to spawn the enemy, for example when using the code “set [fire] to (create enemy…)” the “fire” variable is the one you are using, but once you make more than one sprite, that variable only effects the most recent enemy created. I suppose if you spawned the enemies and their hitboxes in the same order, you could use that order in the “array of all sprite of kind…” block to tell which hitbox belongs to which sprite, but that’s a bit complicated and, depending on how you are using sprite types, etc, it may not work for you. The simplest solution I can think of is to make your own hitbox code that loops through each sprite and checks if one of the other things is within a certain x/y range of the sprite. That way you wouldn’t be using additional sprites just as hitboxes, which is a very resource intensive way of doing something that could be a lot less laggy when done another way.

3 Likes

I have actually made an extension called “sprite basics” to make it easier to handle this sort of thing.

Here’s a screenshot of the proximity block and how to use it:

3 Likes