- Home /
The question is answered, right answer was accepted
Raycasting on mask doesnt work
Hello!I have a little problem.I'm using raycasting for my player to move(Click to move),but there are some elements on screen wich down want to be hitted by a raycast(Example:I click on UI elements and my player moves towards that click position wich i dont want).I tried using layerMasks for this but it's not working for me somehow.I want my raycast hit only background image so i set it to layer BG wich is 8 but when i use the following code the ray casts on every mask but I need it to cast only on 8th layer. In my code I set layermask to 1<<8 but still not working. Code:
void Update () {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0)) {
if(Physics.Raycast(ray, out hit, 1000,1 << 8)){
target = new Vector2(hit.point.x,hit.point.y);
}
}
transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
}
Thank you for your attention!
Answer by equus_ligneus · Oct 21, 2015 at 01:53 PM
First of all, there is nothing wrong with your raycast code and it should hit everything on the layer with index 8 (which is the 9th layer). However, GUI works slightly different.
The new Unity UI System (UGUI) does not block raycasts in the sense of being a physical object with a collider (TL;DR: you can't just raycast against a Overlay-Type GUI). But there is a way to check whether the mouse cursor is over a UI-element: Asking the EventSystem. In every scene with a Canvas there's an EventSystem. It has a public method IsPointerOverGameObject() that returns true whenever there is a UI-Element under your mouse cursor. So what you do is:
EventSystem eventSystem = FindObjectOfType<EventSystem>();
// there is no event system (no gui) or no gui element under the cursor
if(eventSystem == null || !eventSystem.IsPointerOverGameObject())
{
// do your raycast
}
To avoid confusion: EventSystem is in namespace UnityEngine.EventSystems so you need to include this namespace in the beginning of your file.
I further recommend saving the EventSystem reference as a variable of your class, FindObjectOfType () is a slow operation.
Answer by TH_Unity · Oct 20, 2015 at 06:55 PM
The code seems right to me. Try to use LayerMask.LayerToName(string name) to prevent using a wrong layer index.
I guess the main problem is Camera.main
. The GUI camera might differ from the camera you are trying to cast the ray on. If you do not have a world GUI the camera would most likely be far away from the GUI. Notice that the Ray you get returned from Camera.ScreenPointToRay(Input.mousePosition) is in world coordinates and depends on the camera's position.
I think you have to use the EventSystem of Unity to catch a click on a GUI element.
Follow this Question
Related Questions
How do I move a raycast back on objects z axis? 1 Answer
Enemy gameObject didn't move 1 Answer
Drawing flat arrows 1 Answer
Place Gameobject within a specific tranform value 3d 3 Answers
Help with casting a raycast C# 1 Answer