Hi
I am slowly building a new NReal app in Unity an hour here and there after my day job (I work in Java, C#, MDX and cube reporting in inv banking by day since 1998!!! :)).
I’ve got my splash screens working and I am currently assembling a UI. This is going well. I added a slider to act as a binary toggle, 2 options so 0-1, and I have laser | hands.
By default the laser pointer works, and I got it to be able to click the toggle and on change it flicks to hands…and they appear OK.
So far so good.
I cannot get it to work so I can just press the slider with a fingertip. I tried adding a rigidBody2D around my toggle, 2dcollider with isTrigger enabled, and an OnTriggerEnter2D function.
This didn’t work. I also tried a BoxCollider around it but this didn’t work. Do I need to add a collider around the hand?
Am I overthinking it? I am learning Unity as I go too.
Code attached to the toggle/slider:
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using NRKernal;
using UnityEngine.EventSystems;// Required when using Event data.
using TMPro;
public class HandLaserSwitch : MonoBehaviour, IPointerClickHandler, IPointerDownHandler
{
public Slider laserHandsSlider;
public TMPro.TextMeshProUGUI debugText;
public GameObject rightHandModel;
public GameObject leftHandModel;
/// <param name="eventData"> Current event data.</param>
public void OnPointerClick(PointerEventData eventData)
{
HandleSliderClick();
}
/// <param name="eventData"> Current event data.</param>
public void OnPointerDown(PointerEventData eventData)
{
HandleSliderClick();
}
public void StartHandTracking()
{
NRInput.SetInputSource(InputSourceEnum.Hands);
NRInput.RaycastMode = RaycastModeEnum.Gaze;
NRInput.RaycastersActive = true;
SetHandsActive(true);
}
public void StartLaserController()
{
SetHandsActive(false);
NRInput.SetInputSource(InputSourceEnum.Controller);
NRInput.RaycastMode = RaycastModeEnum.Laser;
NRInput.RaycastersActive = true;
}
public void SetHandsActive(bool isActive)
{
if (leftHandModel)
{
leftHandModel.SetActive(isActive);
}
if (rightHandModel)
{
rightHandModel.SetActive(isActive);
}
}
/*public void ToggleRaycastMode()
{
Debug.Log("HandTrackingExample: ToggleRaycastMode");
NRInput.RaycastMode = NRInput.RaycastMode == RaycastModeEnum.Gaze ? RaycastModeEnum.Laser : RaycastModeEnum.Gaze;
}
public void SwitchHandVisual()
{
Debug.Log("HandTrackingExample: SwitchHandVisual");
//handModelsManager.ToggleHandModelsGroup();
}*/
public void HandleSliderClick()
{
if(laserHandsSlider!=null)
{
int laserHandState = (int)laserHandsSlider.normalizedValue;
debugText.text = "Slider val: " + laserHandState;
switch(laserHandState)
{
case 0:
{
StartLaserController();
break;
}
case 1:
{
StartHandTracking();
break;
}
}
}
}
private void OnTriggerEnter(Collider other)
{
//Mmm..doesn't seem to work.
debugText.text = "Slider val: " + 3;
}
}