Replace Void OnMouseDown for Android

Hello, I am looking for a code to replace the void OnMouseDown for Android but I have read and it cannot be done, I need you to click on the object to make the animation but I need to know how to program that touching the object activates the animation.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Button : MonoBehaviour
{
    Animator anim;

    void Start()
    {
        anim = GetComponent<Animator>();
    }

    void OnMouseDown()
    {
        anim.SetTrigger("Activate");
    }
}

OnMouseDown works for touch as well as far as I know.

Try the apk with the code and it doesn’t work

@PraetorBlue I agree with you but I think it only works when there are no other touches… Unity combines all the touches and presents them as the Input.mousePosition value, which I think is also what these OnMouseX() events use. BUT: I have not used the OnMouseX() stuff coz I roll all my own touch input.

OP, if you wanna look at some touch input helpers, check out my proximity buttons package. It’s not definitive by any stretch and it doesn’t really directly replace any of the OnMouseX() stuff, but it has a few handy helpers in it like my MicroTouch() class that gives you all the touches, including the mouse, in a single easy-to-digest form.

proximity_buttons is presently hosted at these locations:

https://bitbucket.org/kurtdekker/proximity_buttons

1 Like

If you want to try and direct-replace the OnMouseX() stuff, you will need to:

  1. iterate all Input.touches array (see docs) and look for Touch.Began

  2. get the Camera.main.ScreenPointToRay() for each touch to cast a ray into the scene

  3. do a Raycast() with that ray to find the first thing you hit

  4. do a SendMessage( “MyMouseDown”, SendMessageOptions.DontRequireReceiver); on each collider GameObject you hit

  5. rename your OnMouseDown function to be MyMouseDown (you don’t HAVE to, but I highly recommend you do)

2 Likes