- Home /
how to simulate mouse click with fake cursor
I'm making a 2 player game with Wii controls. Of course for the second player I cannot use the mouse cursor.
For the cursors I will probably use a GUI texture, but I don't know how to let the second user click on gui elements, or 3d elements.
For the 3d elements I thought about raytracing from the x,y coordinates and see which object it collides with, but it would be easeer if I just could simulate clicks all over the game in 3d an gui elements too.
So how can I do that?
To click on 3d objects to screen use this:
Camera class, Screen to viewport
or
Camera class, World to Screenpoint
The above 2 are used for interaction of 3d elements via viewport/screen space and not involving a ray cast to detect.
All GUI button type elements will come with a bool return value:
So once you have identified the area you want to look at, or interacted with the 3d object, you can use the GUI to fire an event to do stuff.
A little costly but accurate way is Physics.RayCast method for identification.
On$$anonymous$$ouseOver() is also an event which gets fired if the script is attached to the object in question/monitored.
Hope it helps.
Thanks
I understand how to get the coordinates from viewport to 3d world. But how can I know if those coordinates intersect with a 3d object without using a ray cast?
Also, you say I can attach a script to make a mouseover event, but I will not be using a mouse cursor... I don't really understand what you mean there.
The screen based detection solution requires you to atleast have a On$$anonymous$$ouseOver() function in the script attached to the object which is to be $$anonymous$$ouse-overed, this is just to fire an event which you can use to update your GUI.
Independently speaking, the Physics.Raycast is what is normally used to detect the hit. Physics.Raycast is put on the camera usually because the center of the camera is the center of the screen when it raycast's. Now if you have many object's close to each other, a raycast will pass through them and you'll go into a proximity issue, that means, a ray cast of magnitude 100 can pass thru 2 spheres of diameter 50, and it will return which ever it hits 1st or might return the 2nd (whichever is closer to the camera), which is un predictable.
So if your scene has no close proximity objects then go with raycast, it should be fine. If you have close proximity, go with "On$$anonymous$$ouseOver()" solution. because $$anonymous$$ouseOver will give accuracy over -What-you-See- vs what you cast-into.
Hope that gives you some insight of how and why both are used.
@Starwalker. The On$$anonymous$$ouseOver
"event" is actually just a Physics.Raycast
call, so neither are "what you see". Physics.Raycast
is sorted via hit distance not object distance, so the only problem you can encounter is when an object is cut off by the near clip plane. Otherwise there are no inaccuracies.
Your answer
Follow this Question
Related Questions
Cursor Disappears In Scene View When Play Mode Is On 2 Answers
Game runs on one screen, cursor wanders onto other screens? 0 Answers
error CS8025: Parsing error 1 Answer
How to make something happen when mouse on side of screen? 1 Answer
Cursor appearance change when mouse over a GUI button 2 Answers