- Home /
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.
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.
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?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
XR Interaction Toolkit OnSelectExit is never called 1 Answer