VR, Oculus Quest 2: How can I change the color by pressing a trigger when ray coming out of the VR controller detects an object?
I am fairly new to Unity and am stuck with scripting. I was originally able to get it to work within Unity, but I am trying to translate it to the VR world.
The original code is below, but when interacting with the controller's Ray in the VR, what would the code look like for "if ray from the controller hit an object and then if the grip is pressed do this action"? My search for documentation was not good and it took me about 3 days to finally ask the question. thank you..
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)) //using physics //out means where we put info stored
{
if (hitObject.transform.gameObject.tag == "Star")
{
highlighted = hit.transform.gameObject;
highlighted.GetComponent<Renderer>().material = highlightedM;
}
}
Answer by rh_galaxy · Mar 23 at 12:39 PM
This answers how you can get input from the controllers and get the position and rotation of the right controller. You will have to draw the ray your self using that info, don't know if that is done somewhere else or if it is missing in your code.
internal Vector3 vPosition;
internal Vector3 vDirection;
internal Quaternion qRotation;
internal Vector3 vHitPoint;
void Update()
{
//get inputs
UnityEngine.XR.InputDevice handRDevice = InputDevices.GetDeviceAtXRNode(XRNode.RightHand);
bool triggerRSupported = handRDevice.TryGetFeatureValue
(UnityEngine.XR.CommonUsages.trigger, out float triggerR);
{
bool posRSupported = handRDevice.TryGetFeatureValue
(UnityEngine.XR.CommonUsages.devicePosition, out Vector3 posR);
vPosition = transform.TransformPoint(posR); //to world coords
bool rotRSupported = handRDevice.TryGetFeatureValue
(UnityEngine.XR.CommonUsages.deviceRotation, out Quaternion rotR);
vDirection = rotR * Vector3.forward;
vDirection = transform.TransformDirection(vDirection);
qRotation = Quaternion.LookRotation(vDirection);
}
bool bTigger = (triggerR > 0.5f);
//check where we are pointing
RaycastHit oHitInfo;
if (Physics.Raycast(vPosition, vDirection, out oHitInfo, 400.0f))
{
vHitPoint = oHitInfo.point;
if (bTigger && (oHitInfo.transform.gameObject.tag.CompareTo("Star") == 0))
{
//hilighting
//...
}
}
else
{
vHitPoint = vPosition + vDirection * 20.0f; //set to max distance
//no hilighting
//...
}
}
(I have a cylinder that I match to the ray like this:)
oRayQuad.transform.SetPositionAndRotation((vHitPoint + vPosition) / 2.0f, vDirection);
oRayQuad.transform.Rotate(new Vector3(90, 0, 0));
oRayQuad.transform.localScale = new Vector3(0.05f, ((vHitPoint - vPosition).magnitude - 0.07f)/2.0f, 0.05f);
Hi - thank you very much for this. I really appreciate it. I just noticed that I forgot to mention that I am using XR interaction Toolkit (using UnityEngine.XR.Interaction.Toolkit) and a new input system.. Essentially I was wondering how to convert input from mouse to input from Oculus Quest 2 controller. (ex. mousePosition and mouseGetDown)
at the moment I'm doing below to activate the controller (I'm not sure if it is correct though..)
private ActionBasedController controller;
void Start()
{
controller = GetComponent<ActionBasedController>();
controller.selectAction.action.performed += Action_performed;
}
Your answer
Follow this Question
Related Questions
VR Oculus quest and collision 0 Answers
Developing for VR and Multiple Platforms 0 Answers
Making KeyCodes Equal to Touch Triggers? 1 Answer
Iphone as a Web Camera VUFORIA 2 Answers