Rayshot vs Collider for collision detection...

For things such as bullets / missles, which is better for performance in the Update loop?

To shoot a ray forward, if an object is less than a certain amount away, register a blow up script.

Or else to just attach a sphere collider on the tip of the object, and use the OnTriggerEnter to figure out if a collision happened.

I have been using a sphere collider, and I’m happy so far, but I have seen other people use a ray cast.

I am mainly looking at the best for performance. Lets say I would be doing this for 100 objects at once, for theory sake.

If you go the collider route then why use a sphere collider? I can’t testify to this, but surely a cube collider is quicker and more efficent at detecting collisions? And for small objects like bullets and missiles I don’t see why you would need a sphere collider because you’re only interested in the inital collision and not bouncing or things like that.

Bullets tend to travel really fast as well as being tiny so in my experience they tend to fly straight through things with the collider approach. Using raycasts work much better for that purpose.

For missiles I’m using rigidbodies with colliders. This gives me added niceties such as being able to shoot the missile out of its current trajectory etc. :slight_smile:

Thank you for the response and suggestions!

As far as math goes, I think the sphere is actually the cheapest of the colliders.

hit = Vector3.Distance(transform.position, other.transform.position) < radius;

With cylinder you have to check distance+height

With cube you have to check (+x, -x, +y, -y, +z, -z) or (-x, +x, -y, +y, -z, +z)

With mesh, you need to see if the point lies outside of every face normal.

So I thought the sphere was the fastest. I could be wrong of course. :slight_smile: I don’t know how Unity handles these internally.

The missle does contain a rigid body, but it doesn’t contain a mesh collider. Instead I put the sphere collider on the tip of its nose, and set the ‘Is Trigger’ to true.

So I propel the missle using the rigid body forces, but then use the sphere collider for the collision detection. I will not be having things shoot them out of the sky, such as in your case.

I agree with you though, it makes sense to use the least expensive collider in this situation, does anyone have any idea which one it is?

Great topic. I’m more listening in than offering advice, but here’s another possible tool for you:

Use Physics.CheckSphere to test if an object is within radius. Not sure how efficient that is.

I’d use distancesquared to avoid a square root, but even then spherical distance is three multiplies while cube is (an average of) less than three comparisons (most objects will be outside in more than one dimension, and you’ll get to the correct test on average in three attempts or fewer if only one dimension is out).

Even so – unless you’ve gone nuts, the overhead will be trivial.

For some reason when I started using Unity I thought the colliders went like this from cheapest to most expensive:

box
sphere
capsule
mesh

I thought I had seen it in the official documentation, but I can’t find it now. :slight_smile: I never thought of it in terms of math, but what you’re saying and what I could find on the forums seem to acknowledge that it is in fact:

sphere
capsule
box
mesh

Guess I will have to switch out some colliders in my game for that added tiny bit of performance. :slight_smile:

I really want to see the best answer for this too.

Specially to use in network games, such shooters online games.

Which is the best method to get better performance in single player and multiplayer?!

:smile:

It would be nice if there was a “Best Practices” entry on the Wiki that discussed the most performant way to do common things.

Over the course of time with questions like these, the section would be a valuable reference for people trying to squeeze out that last FPS…

-Will

I would have assumed the cube would be best performance wise, so it would be great to know what is truly best performance wise.

podperson, that is a good point. Math vs comparisons. That makes me think a cube is the most effectient, as Twiik said. It looks like your first list is probably right:

box
sphere
capsol
mesh

With box you just compare values, where with the sphere you actually have to compute somethings.