Question by
Dan6erbond · Feb 03, 2017 at 08:52 PM ·
androidmobiletouchscreenpc
How can I block Physical Raycasts from going through UI Elements?
Hi
I made a simple Raycast Script to detect which 3D Object was clicked on, I want the UI to block the Raycasts on mobile & PC, I was only able to get one of each to work but not both...
How can I do it?
My Script: using System.Collections; using UnityEngine.UI; using UnityEngine; using UnityEngine.EventSystems;
public class MouseManager : MonoBehaviour {
//working Raycast Script for Mobile Devices
RaycastHit hit;
public GameObject CountrySelected; //Country GameObject
//Country Selected Button
public Text CountryName; //Text to Display selected Country
public GameObject CountryNameText; //Text to Display selected Country - GameObject to set active
void Start(){
CountryNameText.SetActive (false);
}
void Update () {
if (Input.GetMouseButtonDown (0)) {
if (Input.GetMouseButtonDown (0) || Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) {
if (UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject () || EventSystem.current.IsPointerOverGameObject (Input.GetTouch (0).fingerId)) {
return;
}
else {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit, 10f)) { //Raycast
CountrySelected = hit.collider.gameObject; //CountrySelected is the GameObject selected with the Mouse
}
}
}
}
CountryName.text = "" + CountrySelected.name;
if (CountrySelected.name == "World") {
CountryNameText.SetActive (false);
}
else {
CountryNameText.SetActive (true);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Multi-touch game not working 0 Answers
Unity 3D UI Button Not Working When Touched (Mobile)? 1 Answer
Touch screen not working on Android build? 0 Answers
Augmented Reality movement mobile 0 Answers
Game is too fast in Mobile 0 Answers