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!!!");
}
}
I don't have any error messages or warnings in the console.
This is the repository, branch develop
:
https://github.com/emicalvacho/MapaMentalAR.git
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:
(and yes I just used a "dynamic image" target :D )
You can fix this in a few steps:
Disable RaycastTarget
on the Text
component:
this way it doesn't interfere with the pointer raycast
For getting a 3D collider your Camera
should have a PhysicsRaycaster
component attached:
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
Now I can add and click on the cubes:
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
.