There is a script that creates a ray when clicking on the mouse.

ray = camera.ScreenPointToRay (Input.mousePosition); 

There is also a Canvas on stage, and a Button in it. When the button is pressed, a beam is also created, which I do not need. How not to create a beam, only when you press the button?

  • And the user sees a ray? Is he on the stage visually drawn or not? ...... You can, in principle, in any case, check at the beginning, if (Hit.collider.gameObject.CompareTag("button")) return; that is, if a button is caught, then do nothing - Alexey Shimansky
  • @ Alexey Shimansky thus hit does not see the button, that is, for some reason the ray does not cross it - Nikita Medvedev
  • @ NikitaMedvedev which one? There you should use Physics.Raycast and not just if - Alexey Shimansky
  • Something like this: RaycastHit hit; Ray ray = camera.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { if (hit.gameObject.CompareTag("button")) { return; } //здесь действия не связанные с кнопкой } RaycastHit hit; Ray ray = camera.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { if (hit.gameObject.CompareTag("button")) { return; } //здесь действия не связанные с кнопкой } RaycastHit hit; Ray ray = camera.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { if (hit.gameObject.CompareTag("button")) { return; } //здесь действия не связанные с кнопкой } inspired by docs.unity3d.com/ru/current/Manual/CameraRays.html - Alexey Shimansky

2 answers 2

If you are using a standard UI (and judging by the Canvas, it is) then EventSystem.IsPointerOverGameObject will help you. This function tells you whether the mouse pointer is on or a touch-touch over the UI object. Only for objects above which the mouse should be detected, set the RaycastTarget flag to identify them. How I caught the mouse events on the menu to turn off gestures when we work with the UI. if we work with Touch, we use !EventSystem.current.IsPointerOverGameObject(touch.fingerId) for each tach. For the mouse, passing without the !EventSystem.current.IsPointerOverGameObject(touch.fingerId) parameter will determine the click position for the left mouse button.

eg:

 if (touch.phase == TouchPhase.Began && !EventSystem.current.IsPointerOverGameObject(touch.fingerId)) { MakeWork(); } 
  • Well, I'm waiting .. - Nikita Medvedev

I am not familiar with Unity, therefore in general terms:

First check that the cursor is over the GUI. If yes, then do nothing. If not, then create a beam and then do with it what you need.

  • I would be happy, but I do not know how. - Nikita Medvedev