Raycast Performance

What is more expensive on performance?

(A)

 if (collider.Raycast(ray, out hit, 1000))
    {
    }

(B)

   if (Physics.Raycast(ray, out hit, 1000))
    {
    }

I plan to have enemies and shoot at them with raycast. (A) I can either put the if statement inside the enemy as a collider and it would check if it got hit, or (B) I can put the if on the player and send a message to the hit enemy.

Which is better?

My logic says that (B) is better, but I have no real knowledge between collider.Raycast and Physics.Raycast.

(C)

if(Input.GetMouseButtonDown(0))
{
    ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			
    if (Physics.Raycast(ray, out hit, 1000))
    {
    }
}

If I do part (C) above.
How many raycast am I actually sending? One?

You need to test performance with your REAL environment.
But well, I had taken a simple test on Physics.Raycast for reference;

I created a 20002000 terrain, with dozens 10010*100(xyz) cubes over it, then every frame I cast a lot of Raycast from [x, 1000, z] downward (x and z are among [0,2000]).

2000 rays → 3ms per frame for raycast

10000 rays → 15ms per frame for raycast

this test is executed on a Notebook with i5-480M and GT445M.

I suppose that collider.Raycast is faster than Physics.Raycast, since only one collider is verified, but the extra code required to make this alternative work is probably way more expensive: when the player shoots, all enemy colliders must do a raycast to see whether they are hit. The physics engine is highly optimized and compiled to machine code, thus it probably runs a single Physics.Raycast much faster than a couple collider.Raycast called via script (even compiled to CIL, our scripts run much slower than machine code). You could reduce the enemy list only to those in front of the player, but this would also require extra script code… I suppose this is one of these cases where a man can’t win the machine.

Collider Raycast probably won’t do what you need (so who cares how fast it runs.) The purpose of a typical raycast is to check for unknown objects blocking the line from me to the target. Using the layer system, you can have a raycast skip certain objects – maybe of all the thorny bushes have colliders to stop movement, but a lazer blast goes right through them.

Collider.Raycast says it skips everything except that one object, so it’s no good for “is anything blocking the way?” It might be good is maybe you know you are close enough to stab someone, but want to check exactly where the put the ichor spray.

For speed, finding and checking all the other objects in the scene takes time. A shorter raycast distance speeds it up by ruling out lots of things quickly. Using layers to skip (or Collider.raycast to skip all but one thing) is going to also speed you up.