- Home /
Question by
ajaykrishnasjcet · Feb 10 at 03:26 PM ·
raycastraycastingraycasthit
How can I prevent my raycast from passing through UI in mobile touch
==========================================================================================================================================================================================================================================================
var Data = UnityEngine.InputSystem.Touchscreen.current.primaryTouch.ReadValue();
if (Data.isPrimaryTouch == true&&Interactable&&Time.timeScale==1)
{
Ray ray = MainCamera.ScreenPointToRay(Data.position);
RaycastHit hit;
if (!EventSystem.current.IsPointerOverGameObject(fingerId))
{
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.gameObject.GetComponent<OnClickButton>() != null)
{
hit.collider.gameObject.GetComponent<OnClickButton>().OnClick();
}
if (hit.collider.gameObject.GetComponent<IPlayableObject>() != null)
{
hit.collider.gameObject.GetComponent<IPlayableObject>().OnInteraction(hit.collider.gameObject);
}
}
}
}
==================================================================================== this is my code unfortunately it doesn't work
Comment
Answer by fletcherhcobb · Feb 10 at 06:04 PM
Use a layermask in the raycast. Have a layer for UI which you won't hit with the specific raycast. Check the documentation for raycast and layermask if you need more details.