- Home /
Player and Enemy collisions: Where should the Trigger/Collision method go, on the enemies or on the player (performance-wise)?
Hi, I am creating a fairly simple 2D Platformer game and I was wondering if each instance of a deadly obstacle (i.e. bomb/mine) should have a script with the OnTriggerEnter2D method checking if what it collides with has the tag "Player" or if my player should have a script with the OnTriggerEnter2D method checking if it collides with something with the tag "Obstacle". Either way, I want to have the obstacle be destroyed and the player to die. Most of the obstacles will be floating (so not colliding with environment) and only need to worry about player collisions. Currently, I have scripts on all the obstacles that check for player collisions and it works but I was wondering if it was better performance-wise if the player did the collision checks, especially when dozens of obstacles are in the scene.
Hopefully what I am asking makes sense. Thank you in advance!
If collision check is written in the OnCollissionEnter then there is no much performance issues as it will work pretty fine on both sides. Here I guess you need to look at this from the point of logic, as I see it might work something this way "on hit bomb checks if hit object implements interface IDamageReceiver if so, then invoke method TakeDamage(amount)" (feature here is that player code will know nothing about bombs, and that makes your code more flexible).
Answer by mafima · Nov 30, 2017 at 01:33 PM
player because there is often only 1 player. 1 collision check is as much more efficient how much enemies you have. if you have 1000 ememies, that check for 1 player every update, than performance will die. collider in general are performance hungry, especcially if you have mesh colliders. i made the experience that replacing 500 mesh colliders with box colliders could double your FPS.
so, if you just want to change PLAYER based variables, yes, by all means go with checking it in the player. hope it helps :) click on "stats" in the Game window on the top right to see the performance.