Hi all. My game has one player avatar and can have many enemies simultaneously on screen.
Is there a difference between the following two (pseudo code) approaches to checking for collisions (player with enemy), which one is better (are they basically the same)?:
A only the player object checks for collisions, calls enemy’s function for specific reaction
PlayerObject
if (colliding with enemy){
create reference to colliding enemy;
reference.DoMySpecificCollisionThing();
}
B every single enemy instance checks for collisions, handles everything
EnemyClass
if (colliding with player){
DoMySpecificCollisionThing();
}
Solution B seems more straightforward, but I’m wondering if there’s a significant performance penalty to having many objects checking for collision every frame, or if that’s what’s happening either way as soon as they have a collider component.
Thanks for sharing your thoughts on this, I guess I’m new to object oriented thinking.
The pseudocode you have posted is basically the same as in either case.
If you have N enemies and 1 player, you are doing N collision checks.
The performance is the same for A and B as Collisions will be calculated for all collider objects every physics update no matter which scripts they have on. If it makes the most sense for you to use B for your type of game, then use B. =)
I would do A if the Player specifically did something to the enemy, and B if the enemy just reacts from encountering the player.