user13625527
user13625527

Reputation:

Unity Touch Count ignore Ui input

I have a simple game where when user touches the screen, player jumps. I have implemented this using touchCount as when touchCount =1 then player jumps. But the problem is that there is a button on the screen so when user presses a button, touchCount is validated and player jumps. So how to enable player jump only when user is touching the non ui part of the screen. Thanks in advance.

Upvotes: 1

Views: 3618

Answers (4)

ridone akash
ridone akash

Reputation: 1

if (!EventSystem.current.IsPointerOverGameObject(Input.touchCount) && !IsPointerOverUIObject() && Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)

{

   // UI Element touch will not reach here. You can work with your touch 

}

// Also add this Method to your code

 private bool IsPointerOverUIObject()
    {
        PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current); eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);

        List<RaycastResult> results = new List<RaycastResult>();

        EventSystem.current.RaycastAll(eventDataCurrentPosition, results);

        return results.Count > 1;

    }

Upvotes: 0

Bakkalzade
Bakkalzade

Reputation: 1

I had a function to run my character. but i was dealing with the same problem. there was a bunch of buttons that must be ignored by touch count.

here is my code:

if (Input.touchCount > 0)
{
   run()
}

I have a class to store Main panel, named as GameController. I add a script (btnUI) to the Main Panel that contains buttons. Thanks to the UI system of Unity you can controll wheter you touch on a button or a blank point on the panel.

Here is the code. With IPointerDownHandler on click situation can be designed.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class btnUI : MonoBehaviour, IPointerDownHandler
{
   public void OnPointerDown(PointerEventData eventData)
    {
        GameManager.mainPanel.SetActive(false);
    }
}

In your situation this code may not work fine you can just add IPointerUpHandler like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class btnUI : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
    public void OnPointerDown(PointerEventData eventData)
    {
        GameManager.mainPanel.SetActive(false);
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        GameManager.mainPanel.SetActive(true);
    }
}

I changed my code to:

if(!GameManager.mainPanel.activeSelf)
{
    if (Input.touchCount > 0)
    {
        run()
    }
}

With this code if you tap buttons main panel will be stay active and if you touch any blank point main panel will be deactive and run function will be executed.

You can use a parameter on variable instead of checking wheter the panel active or not.

Upvotes: 0

derHugo
derHugo

Reputation: 90639

You can add a check if the touch is happening on UI using EventSystem.current.IsPointerOverGameObject

Example usage from the API:

// Check if there is a touch
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
    // Check if finger is over a UI element
    if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
    {
        Debug.Log("Touched the UI");
    }
}

Using Linq Where you can then use this condition as a filter in order to only take into account those touches that are not over UI using e.g.

// Get all touches that are NOT over UI
var validTouches = Input.touches.Where(touch => !EventSystem.current.IsPointerOverGameObject(touch.fingerId)).ToArray();

// This is basically a shortcut for writing something like
//var touchesList = new List<Touch>();
//foreach(var touch in Input.touches)
//{
//    if(!EventSystem.current.IsPointerOverGameObject(touch.fingerId))
//    {
//         touchesList.Add(touch);
//    } 
//}
//var validTouches = touchesList.ToArray();

if(validTouches.Length == 1)
{
    // Your jump here
}

Upvotes: 2

Beanie
Beanie

Reputation: 179

This is for android right?>

I'd recommend using ui buttons built in to unity to define the active position, this should autometically work for android I think.

or u could make an if statement for the position of the touch so if the position is lower or higher than a certain point it will not activate.

Upvotes: 0

Related Questions