- Home /
Detect touches on ui element
I have a panel with the following script attached. All I want is to rotate the panel when moving my finger over it and open another menu when tapping it. The problem is that it works wherever I tap (blocking me from interacting with other buttons). So how could I detect touches on this specific object?
![public class RotateChY : MonoBehaviour {
bool menuEnabled = true;
bool moving = false;
[SerializeField] float rotationSpeed = 1f;
[SerializeField] GameObject chHolder;
[SerializeField] GameObject chSelectionScreen;
private void Start() {
chHolder.SetActive(true);
chSelectionScreen.SetActive(false);
}
private void Update() {
if(Input.touchCount == 1){
Touch screenTouch = Input.GetTouch(0);
Debug.Log(EventSystem.current.currentSelectedGameObject + " is the current selected object");
if(screenTouch.phase == TouchPhase.Moved) {
menuEnabled = false;
transform.Rotate(0, -screenTouch.deltaPosition.x * Time.deltaTime * rotationSpeed, 0);
}
else if(screenTouch.phase == TouchPhase.Ended) {
if(menuEnabled) {
openChSelectScreen(true);
} else {
menuEnabled = true;
}
}
}
}
public void openChSelectScreen(bool val) {
chHolder.SetActive(!val);
chSelectionScreen.SetActive(val);
Debug.Log("called with " + val);
}
}][1]
[1]: /storage/temp/184508-screenshot-2021-08-08-122745.png
I have also tried with this statement, to check if the tapped object is my panel, but it didn't work at all.
if(Input.touchCount == 1
&& EventSystem.current.IsPointerOverGameObject(0)
&& EventSystem.current.currentSelectedGameObject != null
&& EventSystem.current.currentSelectedGameObject.CompareTag("CharacterHolder"))
Answer by Tinx-Development · Aug 09, 2021 at 07:22 PM
You could use an Event Trigger component and add an Event Like PointerEnter / Exit or Pointer Down / up https://docs.unity3d.com/2018.3/Documentation/ScriptReference/EventSystems.EventTrigger.html
I tried but for some reason it does not work... The Event Trigger component is not usually working in Unity Remote, only when I build the game, but still it's not working with my panel, just with the buttons.
public class RotateChY : MonoBehaviour{
bool menuEnabled = true;
bool tapping = false;
[SerializeField] float rotationSpeed = 1f;
[SerializeField] GameObject chHolder;
[SerializeField] GameObject chSelectionScreen;
private void Start() {
chHolder.SetActive(true);
chSelectionScreen.SetActive(false);
}
private void Update() {
if(tapping) {
if(Input.touchCount > 0) {
Touch screenTouch = Input.GetTouch(0);
if(screenTouch.phase == TouchPhase.Moved) {
menuEnabled = false;
transform.Rotate(0, -screenTouch.deltaPosition.x * Time.deltaTime * rotationSpeed, 0);
}
else if(screenTouch.phase == TouchPhase.Ended) {
if(menuEnabled) {
openChSelectScreen(true);
} else {
menuEnabled = true;
}
}
}
}
}
public void openChSelectScreen(bool val) {
chHolder.SetActive(!val);
chSelectionScreen.SetActive(val);
}
// public void OnPointerDown(PointerEventData eventData) {
// tapped = true;
// Debug.Log("tapping");
// }
// public void OnPointerUp(PointerEventData eventData) {
// tapped = false;
// Debug.Log("not tapping");
// }
public void pointerDown() {
tapping = true;
Debug.Log("tapping");
}
public void pointerUp() {
tapping = false;
Debug.Log("not tapping");
}
}
Apparently my taps are not detected on the built version...
Your answer
Follow this Question
Related Questions
How do I get which UI conponent is being touched? 0 Answers
A touch'es state is always Began and the position doesn't change 1 Answer
Holding down UI Button and shooting raycast from touch position doesn't work simultaneously:(( 0 Answers
How to move objects with perspective camera and rotating the camera in 3D? 0 Answers
IsPointerOverGameObject doesn't work on TouchPhase.Ended? 1 Answer