Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MrWy07 · Aug 08, 2021 at 09:29 AM · uitoucheventsystemtouchscreentouchphase

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

screenshot-2021-08-08-122745.png (165.7 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MrWy07 · Aug 08, 2021 at 12:25 PM 0
Share

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"))

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MrWy07 · Aug 10, 2021 at 07:50 AM 0
Share

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");
     }
 }

avatar image MrWy07 · Aug 10, 2021 at 07:51 AM 0
Share

Apparently my taps are not detected on the built version...

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

238 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A touch'es state is always Began and the position doesn't change 1 Answer

Detect UI Button Click Event in Update method 3 Answers

How to move objects with perspective camera and rotating the camera in 3D? 0 Answers

Unity Touch Help! 0 Answers

Touch inputs Angry-bird style 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges