2
\$\begingroup\$

I have a simple game object (3d object) and I want to detect and run some code when gets clicked/tapped.

I tried OnPointerDown/Up/Click but seems to not be working for 3D Objects. With 2d objects (images with raycast targets) it works fine.

How can I do it with a 3d objects in a simple way? Thanks!

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$
  1. Make sure your object has a 3D collider component attached with a suitable shape & size

    Ensure it's not in a layer that ignores raycasts (the defaults will work fine)

  2. Add a script using one of these MonoBehaviour methods:

\$\endgroup\$
2
  • \$\begingroup\$ Using OnMouseDown solved the issue, does it work with native mobile taps? (or only mouse click on unity editor?) \$\endgroup\$
    – fj123x
    Commented Oct 27, 2017 at 14:41
  • 1
    \$\begingroup\$ Yes, it will work on mobile. However, if performance is an issue you might prefer to write your own script that manually listens for Pointer Down events and fires its own ray to identify the object touched. The OnMouseDown message signals Unity to do this in a generic way that's great for prototyping but can be more heavyweight than you might need. \$\endgroup\$
    – DMGregory
    Commented Oct 27, 2017 at 15:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .