- Home /
Trouble targeting GUI with raycast
Hey, i'm working on a simple, interactive data sheet to be used for a tabletop RPG, One of the features I am trying to implement is, if the user right clicks on an input field, the background of the box will turn red.
Currently I'm using a very simple AABB collision on each input field to detect, if the mouse is right clicked, and inside the aabb of the field, turn that field red, however I have 78 input fields and having every single one of them check the mouse's location on right click feels extremely inefficient.
I wanted to try out using a raycast from the camera to detect what field the mouse was over, but it's not working and i'm not sure why.
Is there a cleaner/more efficient way to execute this?
This is the raycast code I have, that isn't working. Any help to get it working or suggestions for better efficiency would be appreciated.
If([right click is pressed]) CheckMouseTarget();
Private(CheckMouseTarget){ Ray ray; RaycastHit hit;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if([targeting an input field]){
ChangeFieldColor();
}
} but I can never get the ray to detect the gui objects. The checkbox 'raycast target' is selected on each one
Why does it need a 2d collider if the 'raycast target' box is selected? wouldn't that accomplish the same effect?