- Home /
I need help mimmicking mouse functinality without mouse events
Hi. My game is a 3d point and click adventure game, and most of the interaction in it is handled via mouse events. I'm porting the game to Ouya, and I want to be able to control the in-game cursor with the controller. I'm thinking about using raycasting to detect whether the cursor is over an interactive object, but I have no idea how to use raycast from a 2d GUI element to a 3d object (or other 2d elements such as in-menu buttons etc). Any help would be greatly appreciated. Thanks in advance!
Answer by Owen-Reynolds · Apr 08, 2013 at 06:39 PM
It's all the same math as mouse raycasts, with one change: swap out ScreenToWorld for ViewportToWorld.
The mouse is in pixels, so the standard code uses Camera.main.ScreenToWorldPoint(mousePosition);
If you look, it says that "Screen" means the input is assumed to be in pixels.
guiText/Textures are placed using 0-1 numbers (which are called ViewPort coordinates.) If you wanted, you could multiply them out to get pixels, same as the mouse. But, may as well use Camera.main.ViewportToWorldPoint(guiMouse.position);
.
Once you have the worldPoint, the ray cast, etc... is exactly the same.