How do I stop a RayCast from going through a UI Button and hitting the collider behind the button?
Some things I tried:
- The collider covers the entire view, but it is located on (0,0,2)
- The button is on (0,0,-5), so in front of the collider from the camera's point of view.
- I noticed a Graphic Raycaster component on the Canvas and I set that to Blocking Objects: All, Blocking Mask: Everything, and Ignore Reverse Graphics: True (true by default)
So every time I click the button, it executes the event, but my player ends up behind the button, because the RayCast also hit the other collider, behind the button.
void Start(){
touchControl = LayerMask.GetMask("TouchControl");
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButton(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100, touchControl))
{
Destroy(GameObject.FindGameObjectWithTag("Destination"));
CalculatedDestination = Instantiate(Destination, hit.point, Quaternion.identity);
}
}}