Hello and thanks in advance. The XRController component allows you to select which controller input to use for Select Usage, Activate Usage, and UI Press Usage. I have a situation where I need to Select, Activate or UI Press via script (without the player pressing a button). How would this be accomplished?
Check out the hover to select option under “Selection Configuration” on the XRRayInteractor. I don’t know the specifics of your use-case or setup but that might be what you are looking for.
Unfortunately this is not what I need. I really need to do a UI Press without actually pressing the button (via script). I’m looking through the XRRayInteractor now but I’m not understanding how the activations happen yet.
For clarification I want to aim my XRRayInteractor at some interactable object. Then I want to activate the “select” action between the interactor and the interactable. However I do not want to activate select via controller input. Rather I want to activate the select action via script depending on some condition. For even more context the interactable is a Canvas UI component with a TrackedDeviceGraphicRaycaster component.
Okay, look at InteractionManager.ForceSelect. As long as you have a reference to the interactable and the interactor (and they are registered to the same interaction manager) you can cause the selection to happen.
Thanks! Yeah I just ran a test on this but no luck. I put an XR rig in the scene along with a Grab Interactable. I put a test script on the XRInteractionManager object that holds references to my XRController (interactor) and my Grab object (interactable) and my XRInteractionManager. Everything is pointing to the same XRInteractionManager. I point my controllers at the object, flip my bool, call ForceSelect() and nothing happens. Were you able to get it working this way? Thanks for your help.
My test script:
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
public class Test : MonoBehaviour
{
public bool go;
public XRBaseInteractor interactor;
public XRBaseInteractable interactable;
public XRInteractionManager interactionManager;
void Update()
{
if (go)
{
interactionManager.ForceSelect(interactor, interactable);
Debug.Log("Go!");
}
}
}
Yes, I use it in my Ai scripts so I know it works, it is also used in the package for the “starting Selected Interactable” field. Make sure your interactor and interactable share an interaction layer. Your code there is also going to be forcing the selection every frame that “go” is true. I would change it to be
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
public class Test : MonoBehaviour
{
public bool go;
public XRBaseInteractor interactor;
public XRBaseInteractable interactable;
public XRInteractionManager interactionManager;
void Update()
{
if (go)
{
interactionManager.ForceSelect(interactor, interactable);
Debug.Log("Go!");
go = false;
}
}
}
Hmmm. My XRController and my GrabInteractable layer masks are both set to “everything.” I toggled the bool to false like you suggested. Still doesn’t work. Not sure what I’m missing.
Edit: I misunderstood how the layer masks work. It works now. Thanks much. I still need to check if this works with Canvas UI interactions but this is a good start.
Edit: Nope I was wrong. It’s still not working. I think I accidentally selected with my grip button.
So still not working. Another variable: I’m using Device Based XRControllers and Rig. Not sure if this matters. As I understand it, when I click go in the editor I should see the Grab Interactable Cube fly and attach to the controller. Nothing is happening. The layer masks are good. Not sure what’s going on.
Found out what was going on. Apparently the interactable was instantly deselected. Changing the “Select Action State” on the Ray Interactor to Sticky or Toggle keeps the interactable selected.