Physics.Raycast versus OnMouseDown()

Hi,

until now I check many objects if the were clicked/touched using just one global script:

if (Physics.Raycast (ray, hit, 10000, mask)) {
hit.transform.GetComponent(Clicked).Clicked();
}

This works fine but is a bit unhandy in some cases to me, because:

  1. I have to attach the Clicked.js to each clickable object.
  2. I have to use GetComponent(Clicked) in other scripts on this object

I could use OnMouseDown() instead (which is directly accessable in scripts and also works for touches!)
but this impacts performance (yellow console warnings).

What´s your opinion?
Does it still make sense to use Raycast?
Is the OnMouseDown()-impact noticable on lets say 20 objects, especially on iOS?

Thanks!
Carsten

1 Like

OnMouseDown is raycasting behind the scenes anyway, so there shouldn’t be much performance difference.

–Eric

OK, but 20 objects mean 20 “update-checks” versus one update-check

It’s things like this where I break the OOP, just create a touch manager class and do just the one ray cast and store the result in the class to be picked up in your other scripts.