0
\$\begingroup\$

I'm creating a game where hundreds of rockets with colliders attached are shooting at a player. Should I write my script where:

1: The player script reads the collision from hundreds of rocket colliders or 2: Each rocket collider updates information when colliding with the player

Thanks!

\$\endgroup\$
  • \$\begingroup\$ Try it both ways and profile the results. Unity's made for this kind of quick prototyping. If you're just wondering where to put the response to the OnCollisionEnter messages, then it probably won't make a big difference - the heavy lifting is done by the engine in just detecting those collisions, and that has to be done whether the script to react is on the player or the rocket. \$\endgroup\$ – DMGregory Apr 22 '16 at 11:29
1
\$\begingroup\$

After a bit of thought but no testing, it depends on your rockets.

  1. If the rockets are going to hit only the player then check on the script attached to the player and do everything there including destroying the rocket.

  2. If your rockets are going to hit more than the player then it's best to let the rockets do the collision checks on the rocket and have it subract the health of the hit object.

I'l give you an example. I'm working on a game with swords. I had to do it a bit more complex but the sword has the script that gets the object collided with and tells the hit object to subtract its life.

In my case though, the info is sent to an Animation event for further checks.

\$\endgroup\$
1
\$\begingroup\$

After some consideration I'm completely changing my previous answer.

It would be more effective to implement a OnCollisionEnter method on the player that will do logic when a rocket hits him. That way less collision will have to be calculated and the physics engine will only have to keep track of the position of the rockets colliders and the rockets velocity. No need to do any checks or calculations on each of the hundreds of rocket objects.

\$\endgroup\$
  • \$\begingroup\$ It's unclear to me why you claim putting the OnCollisionEnter method on the player would mean "No need to do any checks or calculations on each of the hundreds of rocket objects" - those collision checks against the player still need to be run if you want the player's OnCollisionEnter to be called. If you're talking about mutual collision checks between two rockets, that's not determined by whether they have OnCollisionEnter methods, but by whether their physics layer is configured to interact with itself. \$\endgroup\$ – DMGregory Apr 22 '16 at 11:55
  • \$\begingroup\$ OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider. So I guess if the rockets dont interact with each other its going to be pretty much the same. \$\endgroup\$ – Uri Popov Apr 22 '16 at 12:07
  • \$\begingroup\$ Remember, most of the work is in detecting that the collision happened, which the physics engine does anyway. The method call overhead (with an early out if the object hit is not a type of interest) is likely to be fairly small, but we can profile it to be sure. \$\endgroup\$ – DMGregory Apr 22 '16 at 12:15
  • \$\begingroup\$ Thanks for the responses! I'll run some tests myself, but it seems checking for collisions in the player script would be easier than updating hundreds of scripts. \$\endgroup\$ – Jquist Apr 23 '16 at 1:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.