1
\$\begingroup\$

(Unity 5.1)

My Issue is when attaching a script to a gameObject, all the examples I've found are based with the update() function and it's pretty wierd for me, as a desktop games developer.

Is there anything like "OnMouseDown()" or other functions for a Touch platform game? Or do I must figure it out with Touch.GetInput from the update() function and that's it?

Sorry in advance if my question is a bit cliche, it's just driving me nuts because I can't find anything with the Unity API or code examples, and I just can't believe there is no some "OnTap()" function...

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

There is no event handlers for touch inputs. You can make them if you want. It's not very difficult but I don't recommend doing it like you have described anyway.

Input events should map to the actions to take, rather than the specific input method used to implement that action. Consider if you are making a game for both desktop and mobile. OnTouch doesn't make sense on the desktop and OnClick doesn't make sense on mobile. It's better to have something like an OnSelect event that is called when either a touch or a mouseclick occurs.

Also consider if you might want to allow the user to remap the inputs or even if you decide to change the input mapping yourself during development. OnTouch or OnClick events would create complications and require reworking lots of code, rather than just having OnSelect and changing how it's triggered.

To make your own event handlers, investigate the GameObject.SendMessage() method.

Basically, what you would do is perform your tests for touch or left-click in the Update() method and trigger your OnSelect event when one or the other is detected by using SendMessage.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks a lot, I've been starting to do something similiar, like a detector from the update() function that trigger Touch(position) method. Thanks a lot for confirming my doubts :) \$\endgroup\$ Sep 13, 2015 at 9:27

You must log in to answer this question.

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