Forum Discussion
Gerald
12 years agoExpert Protege
my mouseclick Raycast does not work on the Rift cameras
I do a simple Raycast to see if someone clicked on one of my Menu elements with the mouse. Works just fine, except when the Rift is activated. Now I can't get it to work with the Rift and I don't see what I do wrong, so if someone has an idea it would be very much appreciated:
the code is only for one of the cameras, I check against both since I don't know where the mouse cursor is. This is a stupid thing to do anyway since you normally navigate the menu with the Leap Motion Controller, but they want me to keep it usable with a mouse too ... so this is pretty annoying (I might have to remove Rift support because of this if I can't get it to work :? ... noooooooo ).
the code is only for one of the cameras, I check against both since I don't know where the mouse cursor is. This is a stupid thing to do anyway since you normally navigate the menu with the Leap Motion Controller, but they want me to keep it usable with a mouse too ... so this is pretty annoying (I might have to remove Rift support because of this if I can't get it to work :? ... noooooooo ).
RaycastHit hit;
Ray ray = RiftMenuCam2.camera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (hit.rigidbody != null)
return hit.transform.gameObject;
else
return null;
}
5 Replies
Replies have been turned off for this discussion
- ScsiOverdriveExplorerI haven't done mouse coordinates with the OVR camera yet, but I have implemented a "look at the button to select it" type of interface. One thing that I came across was that I had weird issues when the raycasting was done in Update(). Switching to FixedUpdate() seemed so solve my issues with that, I think it was simple timing issues with the OVR camera updates.
If you aren't getting any ray hits at all I would recommend checking out Debug.DrawRay(), using ray.origin and ray.direction as arguments for that. Then you can visually check if the ray is being drawn at all, and if its in the right direction. - GeraldExpert ProtegeI tried to move it to the fixed update, but since pretty much everything is running in update that had the opposite effect and made even the working raycast unstable in the detection.
I would love to use that debug function, but since I am not sure on how to get from screenpointtoray to a direction from the input.mousePosition I am not able to produce reliable results there. still pretty new to Unity.
thank you for your input though! - ScsiOverdriveExplorer
Ray ray = RiftMenuCam2.camera.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction, Color.red);
I haven't tested that code, but it should work. - CaliberMengskExplorerI have a feeling your main issue is that Input.mousePosition is weird in unity. The y axis is bottom based (meaning that the bottom of the screen is 0, and the top is however tall your game window is)
This has bugged me for a while since every single other pixel based system is top left based.
Either way, I don't see anything wrong with your code beside this. It's easy enough to fix, but is still annoying that you have to fix it in the first place.RaycastHit hit;
Ray ray = RiftMenuCam2.camera.ScreenPointToRay(new Vector3(Input.mousePosition.x, Screen.height-Input.mousePosition.y));
if (Physics.Raycast(ray, out hit))
{
if (hit.rigidbody != null)
{
return hit.transform.gameObject;
}else{
return null;
}
}
Try that out and see if it works for you. This took me quite a while to figure out. It effects all raycasts with a mouse click, and even messes things up if for some reason you wanted a custom gui element. - FinsternHonored Guest
Ray ray = RiftMenuCam2.camera.ScreenPointToRay(Input.mousePosition);
That line is your problem.
Screen point to ray is using the entire 1280x800 to produce mouse coordinates. If you move the mouse from left to right you'll see it move across the screen once, this is it hitting the middle of the screen (which is the end of your left eye's visiablity), then you'll see it starting at the start again and reaching the end of the screen (this is it entering the right half of the rifts screen and exiting again(for the right eye/half of the screen).
I fixed it by having the reticle as a 2d gameobject that always faces the camera, it's position is equal to a raycast hit and its scale increase as the distance does
If you look at the OVRCrosshair script that comes with the Oculus Unity SDK you can use that as a base for implementing mouse control.
Quick Links
- Horizon Developer Support
- Quest User Forums
- Troubleshooting Forum for problems with a game or app
- Quest Support for problems with your device
Other Meta Support
Related Content
- 11 months ago
- 10 months ago
- 2 years ago