Search code examples
c#unity-game-engineaugmented-realityvuforiaonmousedown

OnMouseDown not work in Vuforia - Unity C#


I'm testing AR in Unity with Vuforia and I can't have the event OnMouseDown() working correctly.

It happens that the first time I hit play it works but just one time. I've already checked that the collider is activated and well positioned. Also I see that the check of the script in the GameObject (Cube in this case) is not enabled, it doesn't even appear like the rest of the scripts when components are made.

This is the code:

using System.Collections.Generic;
using UnityEngine;

public class Click : MonoBehaviour
{
    void OnMouseDown()
    {
        Debug.Log("CLICK!!!");
    }
}

When is in play

When is in scene

In the project

I don't have any error messages or warnings in the console.

This is the repository, branch develop: https://github.com/emicalvacho/MapaMentalAR.git


Solution

  • I just found out: You are always hovering the Hola text on the object .. not the cube. It blocks the raycast!

    How I found out: I wrote a simple script for finding out what is currently hovered:

    public class RayDebugger : MonoBehaviour
    {
        private void OnGUI()
        {
            GUI.color = Color.green;
    
            var hovering = EventSystem.current.IsPointerOverGameObject();
    
            var isHovering = hovering ? "Yes" : "No";
    
            GUI.Label(new Rect(100, 100, 200, 200), $"Is hovering something? - {isHovering}");
    
            if (!hovering) return;
    
            var pointer = new PointerEventData(EventSystem.current) { position = Input.mousePosition };
    
            var raycastResults = new List<RaycastResult>();
            EventSystem.current.RaycastAll(pointer, raycastResults);
    
            if (raycastResults.Count > 0)
            {
                GUI.Label(new Rect(100, 200, 200, 200), $"Currently Hovered: {raycastResults[0]}");
            }
        }
    }
    

    As you can see it is always your "Hola" Text component:

    enter image description here

    (and yes I just used a "dynamic image" target :D )


    You can fix this in a few steps:

    1. Disable RaycastTarget on the Text component:

      enter image description here

      this way it doesn't interfere with the pointer raycast

    2. For getting a 3D collider your Camera should have a PhysicsRaycaster component attached:

      enter image description here

    3. I don't know why exactly but it only works if you use a Perspective Camera. Vuforia somehow seems to have a trouble with an Orthographic one .. understandable because for such a camera no distances exist. So rather switch you camera to Perspective

      enter image description here


    Now I can add and click on the cubes:

    enter image description here


    Btw

    I don't think it worked with Overlay, but I can try.

    as the info box says without a Camera referenced (which is the case in your scene) a ScreenSpace - Camera just behaves equal to a ScreenSpace - Overlay.