• 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
Question by Nisp270998 · Aug 18, 2020 at 05:49 PM · user interfacetouchphasetouches

How to exclude specific touch area!

So I have a code where if the users touches on left-side of screen he turns left and when in right-size he turns right. That is coded in script.

I have placed a mute sound button on right corner. But when ever I press on it, the players turns right as it is sensed that they user touched right side.

I tried using buttons but it became glitchy delaying the actions on onClick.

Is there a workaround for it?

Regards.

  if (rotationType == RotationType.FixedAngle)
                     {
                         
                         //If we have touched the right side of the screen.
                         if (touch.phase == TouchPhase.Began && touch.position.x > Screen.width / 2)
                         {
                             wantedRotation.z += fixedAngle;
                         }
 
                         //If we have touched the left side of the screen.
                          if (touch.phase == TouchPhase.Began && touch.position.x < Screen.width / 2)
                          {
                         wantedRotation.z -= fixedAngle;
                          }
                     }

Comment

People who like this

0 Show 0
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

4 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Spip5 · Aug 18, 2020 at 05:53 PM

Try addin this to your code, it checks if the finger is pointing over a UI element

     public bool IsPointerOverUI(int fingerId)
     {
         EventSystem eventSystem = EventSystem.current;
         return (eventSystem.IsPointerOverGameObject(fingerId)
             && eventSystem.currentSelectedGameObject != null);
     }
Comment
MintArcade

People who like this

1 Show 0 · 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

Answer by Nisp270998 · Aug 18, 2020 at 06:36 PM

@Spip5 This is my code could you help me to properly set the code you mentioned. I am a kinda beginner in this.

Regards.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using DG.Tweening;
 
 public enum RotationType
 {
     FixedAngle,
     FreeRotate
 }
 
 /// <summary>
 /// Player related Input, movement and rotation behaviour.
 /// </summary>
 public class PlayerController : MonoBehaviour
 {
     public GameObject touch1;
 
     //The script responsible for keeping track of the score.
     private ScoreManager scoreManager;
 
     //The script responsible for all common game and UI events.
     private EventManager eventManager;
 
     //The rigidbody attached to the player.
     private Rigidbody player;
 
     
 
     [Header("Player Movement")]
 
     //The behaviour of the player when rotating around the tunnel.
     [Tooltip("The way in which the player rotates around the tunnel interior.")]
     public RotationType rotationType = RotationType.FixedAngle;
 
     //The initial speed of the player.
     [Tooltip("The speed of the player object.")]
     public float speed = 12f;
 
     //The speed at which the object will rotate.
     [Tooltip("The speed at which the player object will rotate.")]
     public float rotationSpeed = 10f;
 
     //The value in degrees that the player will be rotated by if the rotationType is Fixed Angle.
     [Tooltip("The Fixed Angle rotation amount in degrees.")]
     public float fixedAngle = 45f;
 
     [Header("Miscellaneous")]
 
     //The Layer that the threat objects belong to.
     public LayerMask threatLayer;
 
     //Should input be accepted?
     public bool acceptInput = false;
 
     //The initial starting rotation of the player.
     Vector3 wantedRotation;
 
     //The start and end points for the LineCast that will detect the distance to a threat in front of the player.
     private Vector3 detectorPosition;
     private Vector3 endPosition;
 
     private void Awake() {
 
         //Cache the ScoreManager script.
         scoreManager = GameObject.FindObjectOfType<ScoreManager>();
 
         touch1 = GameObject.Find("Mute");
 
         //Cache the EventManager script and populate the necessary variables.
         eventManager = GameObject.FindObjectOfType<EventManager>();
         eventManager.playerController = this;
         eventManager.scoreManager = scoreManager;
 
         //Get the RigidBody attached to this object.
         player = GetComponent<Rigidbody>();
     }
 
     void Update()
     {
        
         if (acceptInput)
         {
             if (!Application.isMobilePlatform)
             {
                 #region KEYBOARD INPUT.
 
                 #region FIXED ANGLE INPUT.
 
                 //If we are using the fixed angle rotationType.
                 if (rotationType == RotationType.FixedAngle)
                 {
                     //If we press the right arrow key, set the new wantedRotation.
                     if (Input.GetKeyUp("right"))
                     {
                         wantedRotation.z += fixedAngle;
 
                         //Tell the AudioManager that we have moved so that it can play the required sound effect.
                         AudioManager.moved = true;
                     }
 
                     //If we press the left arrow key, set the new wantedRotation.
                     if (Input.GetKeyUp("left"))
                     {
                         wantedRotation.z -= fixedAngle;
 
                         //Tell the AudioManager that we have moved so that it can play the necessary sound effect.
                         AudioManager.moved = true;
                     }
                 }
 
                 #endregion
                 #endregion  
 
             }
 
             if (Application.isMobilePlatform)
             {
                 #region TOUCH INPUT.
 
                 foreach (Touch touch in Input.touches)
                 {
                     #region FIXED ANGLE INPUT
 
                    
 
                     if (rotationType == RotationType.FixedAngle)
                     {
                         
                         //If we have touched the right side of the screen.
                         if (touch.phase == TouchPhase.Began && touch.position.x > Screen.width / 2)
                         {
                             wantedRotation.z += fixedAngle;
                         }
 
                         //If we have touched the left side of the screen.
                          if (touch.phase == TouchPhase.Began && touch.position.x < Screen.width / 2)
                          {
                         wantedRotation.z -= fixedAngle;
                          }
                     }
 
                     #endregion
 
                 }
 
                 #endregion
 
             }
         }
     }
 
     void FixedUpdate()
     {
         //Move forward at a constant speed.
         player.MovePosition(transform.position + transform.forward * Time.deltaTime * speed);
 
         //Rotate the player object by the specified angle.
         player.MoveRotation(Quaternion.Lerp(transform.rotation, Quaternion.Euler(wantedRotation), Time.deltaTime * rotationSpeed));
 
     }
 
     //Used to detect a threat or a near miss.
     void OnTriggerEnter(Collider threat)
     {
         //Does the object that we have collided with belong to the threat layer that we have set?
         if (((1 << threat.gameObject.layer) & threatLayer) != 0)
         {
             //Disable the threat we have collided with.
             threat.gameObject.SetActive(false);
 
             //If we aren't accepting input, we do not want to provide feedback.
             if (acceptInput)
             {
                 //If we have collided with a threat, provide visual feedback.
                 Camera.main.DOShakePosition(.3f, .2f, 50, 50);
 
                 //Play the imact sound asociated with a game over event.
                 AudioManager.failed = true;
                 
 
                 //Game Over.
                 eventManager.GameOver();
             }
 
         }
 
     }
 }
 
Comment

People who like this

0 Show 1 · 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 Spip5 · Aug 18, 2020 at 08:07 PM 0
Share
     void Update()
     {
 
         if (acceptInput)
         {
             if (!Application.isMobilePlatform)
             {
                 #region KEYBOARD INPUT.
 
                 #region FIXED ANGLE INPUT.
 
                 //If we are using the fixed angle rotationType.
                 if (rotationType == RotationType.FixedAngle)
                 {
                     //If we press the right arrow key, set the new wantedRotation.
                     if (Input.GetKeyUp("right"))
                     {
                         wantedRotation.z += fixedAngle;
 
                         //Tell the AudioManager that we have moved so that it can play the required sound effect.
                         AudioManager.moved = true;
                     }
 
                     //If we press the left arrow key, set the new wantedRotation.
                     if (Input.GetKeyUp("left"))
                     {
                         wantedRotation.z -= fixedAngle;
 
                         //Tell the AudioManager that we have moved so that it can play the necessary sound effect.
                         AudioManager.moved = true;
                     }
                 }
 
                 #endregion
                 #endregion
 
             }
 
             if (Application.isMobilePlatform)
             {
                 #region TOUCH INPUT.
 
                 foreach (Touch touch in Input.touches)
                 {
                     #region FIXED ANGLE INPUT
 
                     if(IsPointerOverUI(touch.fingerId))
                     {
                         return;
                     }
 
 
                     if (rotationType == RotationType.FixedAngle)
                     {
 
                         //If we have touched the right side of the screen.
                         if (touch.phase == TouchPhase.Began && touch.position.x > Screen.width / 2)
                         {
                             wantedRotation.z += fixedAngle;
                         }
 
                         //If we have touched the left side of the screen.
                         if (touch.phase == TouchPhase.Began && touch.position.x < Screen.width / 2)
                         {
                             wantedRotation.z -= fixedAngle;
                         }
                     }
 
                     #endregion
 
                 }
 
                 #endregion
 
             }
         }
     }
 
     public bool IsPointerOverUI(int fingerId)
     {
         EventSystem eventSystem = EventSystem.current;
         return (eventSystem.IsPointerOverGameObject(fingerId)
             && eventSystem.currentSelectedGameObject != null);
     }
avatar image

Answer by havocipher · Dec 29, 2020 at 03:09 AM

This doesn't seem to work, do you have a solution? Here is my code that doesn't work:

private void Update() {

         if (deviceChecker.isMobile) {
             for (int i=0;i<Input.touchCount;++i) {
             
                 if (Input.GetTouch(i).phase == TouchPhase.Began) {
                     Touch touch = Input.GetTouch(0);
                     cursor.transform.position = touch.position;
                     Vector2 direction = Camera.main.ScreenToWorldPoint(cursor.transform.position) - transform.position;
                     float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
                     Quaternion rotation = Quaternion.AngleAxis(angle - 90f, Vector3.forward);
                     transform.rotation = rotation;
                 }
                 if(Time.time >= shotTime) {
                     Instantiate(projectile, shotPoint.position, transform.rotation);
                     cameraAnim.SetTrigger("shake");
                     shotTime = Time.time + timeBetweenShots;
                     
                     foreach(Touch touch in Input.touches) {
                       if(IsPointerOverUI(touch.fingerId)) {
                             return;
                         
                         }
                     }
                 }
             }
 
          public bool IsPointerOverUI(int fingerId) {
          EventSystem eventSystem = EventSystem.current;
          return (eventSystem.IsPointerOverGameObject(fingerId)
           && eventSystem.currentSelectedGameObject != null);
     }
Comment

People who like this

0 Show 0 · 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

Answer by Klobster · May 07, 2022 at 10:45 AM

I found this worked best for me.

For some reason the final phases of the touch action ( TouchPhase.Ended & TouchPhase.Cancelled) were still dodging the check. Perhaps they are not registering as occurring over the UI. So I had to add the second if statement to specifically to deal with these phases.

 using UnityEngine.EventSystems;
 
 void Update ()
 {
     //Exit if touch is over UI element.
     foreach (Touch touch in Input.touches)
     {
         int id = touch.fingerId;
         if (EventSystem.current.IsPointerOverGameObject(id)) { return; }
         if ((touch.phase == TouchPhase.Ended) || (touch.phase == TouchPhase.Canceled)) { return; }
     }
 }


Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

136 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

Related Questions

TouchPhase interference with two functions 1 Answer

Swipe horizontally and hold finger down to move character? 1 Answer

Check whether touch is held 0 Answers

Weird graphic glitch on certain Android devices (2019.2.8f1) 0 Answers

Get Position of a specific letter in UI Text containing line breaks 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