- Home /
Custom Inspector Raycasting
After some time of googling, I've failed to find anything that helps me on this. I've got a script that needs to detect if the player has clicked on a state or not, but the Debug.Log() I have inside of the Physics.Raycast is never getting called. I can't seem to find the problem.
void OnSceneGUI () {
if (grabbingState) {
int controlID = GUIUtility.GetControlID (FocusType.Passive);
HandleUtility.AddDefaultControl (controlID);
if (Event.current.type == EventType.MouseDown) {
if (Event.current.button == 0) {
Debug.Log ("Help");
Ray ray = HandleUtility.GUIPointToWorldRay (Event.current.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast (ray, out hitInfo, 1000f)) {
Debug.Log (hitInfo.collider.name);
if (hitInfo.collider.tag == "State") {
Debug.Log ("Hit state");
//localState.ConnectedStates.Add (hitInfo.collider.gameObject.GetComponent<State> ());
}
}
}
Event.current.Use ();
grabbingState = false;
}
}
}
Any and all help is appreciated, Cheers.
EDIT: I just thought to include a note - The game is 2D
Answer by WinterboltGames · Jan 20, 2018 at 11:48 PM
At line 8, try Camera.current.ScreenPointToRay(Input.mousePosition);
Camera.current will refer to the editor's camera then...
EDIT: It's 2D Right? then try something like this...
Ray ray = Camera.current.ScreenPointToRay(Input.mousePosition); RaycastHit2D hit2d = Physics2D.Raycast(ray.origin, ray.direction);
EDIT: Try this... Ray ray = HandleUtility.GUIPointToWorldRay( Event.current.mousePosition ); RaycastHit2D hit2d = Physics2D.Raycast(ray.origin, ray.direction);
It's returning the hit2d collider as null (that it hit nothing) when I click on sprites that have colliders, any thoughts?
Thanks, I had tried that before but without the 2D part - I hadn't realized raycast changes in 2D
Your answer

Follow this Question
Related Questions
Dictionary becomes null unexpectedly when using a custom inspector 2 Answers
Can the GameObjectInspector window be customized? 0 Answers
Inspector Overlapping Text Label at a Position 1 Answer
Custom editor window does not show prefab overrides 2 Answers
Bool field in CustomPropertyDrawer isn't clickable? 2 Answers