Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by passofa · Jul 18, 2019 at 10:35 PM · c#raycastvrcursor

Raycast Gaze Based Cursor Hitting Multiple Gameobjects

I'm trying to build a VR gaze-based "mouse". In my simple testing scene I have three cubes (with box colliders enabled but not "is triggered") in front of my camera with my raycast script. I can successfully put my cursor dot on a gameobject, hold a keyboard button, and the gameobject moves on it's x/y axis based on where I look. This is where I'm getting confused. My three game objects are in the hierarchy in this order:
WhiteBox

BlueBox

RedBox


When I have a box grabbed, and I drag it over or behind another box I am getting inconsistent results, sometimes the it will operate like you would imagine a normal mouse clicking and dragging a window would operate, and other times when the dragged box moves in front of another box, the cursor will switch to the one behind. Here's a quick breakdown of how the interactions happen.
White [over] Blue -> grabs blue
White [over] Red -> stays with white
Blue [over] White -> stays blue
Blue [over] Red -> stays blue
Red [over] White -> switches to white
Red [over] Blue -> switches to blue

Here is the code I have attached to the camera:

 public class DrawRaycast : Monobehaviour
 {
     RaycastHit[] m_Results = new RaycastHit[1];
 
     public Camera playerCamera;
     public GameObject cursor;
     public Gameobject _hoveredObj;
 
     void Update(){
         int layerMask = layerMask.GetMask("TheCubes");
         if(Physics.RaycastNonAlloc(transform.position, transform.forward, m_Results, Mathf.Infinity, layerMask) > 0){
             foreach(var result in m_Results){
                 if(result.collider != null){
                 cursor.transform.position = result.point;
                 _hoveredObj = result.collider.gameObject;
                 cursor.SetActive(true);
                 if(Input.GetKey(KeyCode.Space)){
                     Vector3 temp = new Vector3(cursor.transform.position.x, cursor.transform.position.y, _hoveredObj.transform.position.z);
                     _hoveredObj.transform.position = temp;
             }
         }
     }
 }

(apologizes for any simple syntax errors in the code, I couldn't copy and paste it from the source).
I'm getting the feeling that with these issues and a (offtopic) layermask issue, I've messed something up fundamental with my raycasting. So I figured I should start by learning what I've done wrong at this stage and learn from there.

Edit: Completely missed two lines of code. Specifically the if(Physics... and foreach( lines. So like, the most important.

Comment
Add comment · Show 2
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 Deathdefy · Jul 19, 2019 at 06:27 PM 0
Share

It doesnt look like you ever set your results for your raycast array so I assume you are leaving large parts out(which would help us help you).

Ins$$anonymous$$d of pressing and holding using Get$$anonymous$$ey you could use Get$$anonymous$$eyUp & Get$$anonymous$$eyDown. Use the Get$$anonymous$$eyDown to store off the "grabbedObj" and if grabbedObj != null you know to use that ONLY for movement. Once Get$$anonymous$$eyUp fires, you can set grabbedObj to null and you'll no longer be able to move that until you do Get$$anonymous$$eyDown again over an object.

avatar image passofa Deathdefy · Jul 19, 2019 at 09:35 PM 0
Share

Yep, this is what I get for manually hand typing out all the code. I completely missed two lines. However I might still be missing what you're talking about. I'll update the code block and put an edit comment. I initially used Get$$anonymous$$eyDown/Up but it would only move the box a frame/tick then be frozen. So I need to learn how to set the raycast array's results, which I'm assu$$anonymous$$g would need me to set it m_results[1] to null somehow? Because doing "... = null;" and ".Equals(null);" at line 23ish?

0 Replies

· Add your reply
  • Sort: 

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

183 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

XR Interaction Toolkit OnSelectExit is never called 1 Answer

Google Daydream Controller pick up objects 0 Answers

Is There A Way To give Your A.I. A Memory 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