SteamVR Laser OnPointerClick not work with UI Element
GM everyone, I'm having issues with UI and SteamVR laserpointer interaction. I'm trying to use a wrapping class to override the SteamVR input with eventdata and using them in RectTransformUtility.ScreenPointToLocalPointInRectangle. My problem is that is not working at all. What I'm missing? I already followed this guide this guide and now I can't understand why the controller doesnt pass the actions as events. At the moment it only instantiate a cube on that position so I can see it when (and IF) happens, but my main aim is to select a target through the map and make appear an arrow in front of the player that looks to my selected target. I will also add my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEditor;
using Valve.VR;
using Valve.VR.Extras;
public class TargetSelection : MonoBehaviour, IPointerClickHandler
{
public Transform mousePos;
public Camera cameraMap;
public void OnPointerClick(PointerEventData eventData)
{
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(GetComponent<RawImage>().rectTransform, eventData.pressPosition, eventData.pressEventCamera, out Vector2 localCursorPoint))
{
Rect imageRectSize = GetComponent<RawImage>().rectTransform.rect;
Debug.Log("PRIMO IF SUPERATO!");
//localCursorPoint is the distance on x and y axis from the rect center point
//off we add the imageRectSize (by substracting because it's negative) which is the half size
//the rectangle so we can get the local coordinates x and y inside the rectangle
//then we divide them by the rectSize so we can get their ratios (between 0.0 - 1.0)
localCursorPoint.x = (localCursorPoint.x - imageRectSize.x) / imageRectSize.width;
localCursorPoint.y = (localCursorPoint.y - imageRectSize.y) / imageRectSize.height;
CastMiniMapRayToWorld(localCursorPoint);
}
}
private void CastMiniMapRayToWorld(Vector2 localCursor)
{
//we multiply the local ratios inside the minimap image rect with the minimap camera's pixelWidth so we can get the right pixel coordinates for the ray
Ray miniMapRay = cameraMap.ScreenPointToRay(new Vector2(localCursor.x * cameraMap.pixelWidth, localCursor.y * cameraMap.pixelHeight));
Debug.Log("...cast the ray...");
Debug.DrawRay(miniMapRay.origin, miniMapRay.direction, Color.green, 10000);
//we cast the ray through the minimap camera, which will hit the world point that it pointed towards
if (Physics.Raycast(miniMapRay, out RaycastHit miniMapHit, Mathf.Infinity))
{
if(miniMapHit.collider.CompareTag("target"))
{
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = miniMapHit.point;
Debug.Log("WE DID IT! Cube is in " + cube.transform.position);
EditorApplication.isPaused = true;
}
Debug.Log("Target missed. Loser.");
}
}
}
unity.png
(182.3 kB)
Comment