Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by hrn929 · Mar 23 at 10:59 AM · iosscript.vroculusray

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;
         }

     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hrn929 · Mar 23 at 04:42 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

290 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

VR Oculus quest and collision 0 Answers

Developing for VR and Multiple Platforms 0 Answers

Checking if canvas world space is visible in CenterEyeAnchor? 0 Answers

UI flickering when in VR 1 Answer

Disabling unity premade script component,Disabling sealed public script component 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges