2D Energy Generator

Hello guys.

I’m creating some kind of a farming game and it’s 2D.
There are machines you can build and they need an energy source to run.
For that, I was thinking about creating typical generators which you can place wherever you want.
They should have a certain area and everything inside that area is able to run.

I’m asking myself what the best solution would be for this regarding performance also.

My first thought was to give the generator a box collider as a trigger to represent the area. But as far as I know, then I would have to use OnTriggerStay2D to know, which objects are inside. Im totally lost if we talk about performance, so I would like to have some kind of discussion here.

Thx for your time.

So what you want is to use an “onTriggerEnter2D” that checks for items that use electricity as they enter a trigger collider around any given generator. Each item that enters that trigger should be added to a list/array that you can call something like “itemsNearThisPowerSource”.

After that you’ll want an “onTriggerExit2D” that checks “forEach” item in that list to see if any of them match the item exiting trigger. When they do exit remove them from the “itemsNearThisPowerSource” list and unpower them.

You can use OverlapBox2D to check collisions manually.

There is also Collider2D.isTouching()