-1

Google Tango and Unity related question.

Using this tutorial we can place an object on the surface by tapping the screen.

Here is the code:

using UnityEngine;
using System.Collections;

public class KittyUIController : MonoBehaviour
{
    public GameObject m_kitten;
    private TangoPointCloud m_pointCloud;

    void Start()
    {
        m_pointCloud = FindObjectOfType<TangoPointCloud>();
    }

    void Update ()
    {
        if (Input.touchCount == 1)
        {
            // Trigger place kitten function when single touch ended.
            Touch t = Input.GetTouch(0);
            if (t.phase == TouchPhase.Ended)
            {
                PlaceKitten(t.position);
            }
        }
    }

    void PlaceKitten(Vector2 touchPosition)
    {
        // Find the plane.
        Camera cam = Camera.main;
        Vector3 planeCenter;
        Plane plane;
        if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane))
        {
            Debug.Log("cannot find plane.");
            return;
        }

        // Place kitten on the surface, and make it always face the camera.
        if (Vector3.Angle(plane.normal, Vector3.up) < 30.0f)
        {
            Vector3 up = plane.normal;
            Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized;
            Vector3 forward = Vector3.Cross(right, plane.normal).normalized;
            Instantiate(m_kitten, planeCenter, Quaternion.LookRotation(forward, up));
        }
        else
        {
            Debug.Log("surface is too steep for kitten to stand on.");
        }
    }
}

I have buttons on the screen, and when I press them I also place an object on the surface behind it.

How can I avoid it? So if I'm pressing a button I don't want to place an object at the same time.

If I have a function which executes when I tap on the screen, how can I prevent it from executing if I click on a button on the screen?

Rumata
  • 1,027
  • 3
  • 16
  • 47

2 Answers2

1

One way to accomplish this would be to add a tag to your buttons, cast a ray on touch, and see if the ray is colliding with an object with the button's tag.

    if (Input.touchCount == 1)
    {
        // Trigger place kitten function when single touch ended.
        Touch t = Input.GetTouch(0);
        Ray ray = Camera.main.ScreenPointToRay(t.position);
        RaycastHit hit;

        if(t.phase == TouchPhase.Ended && Physics.Raycast(ray,out hit,100))
        {
            if(hit.collider.tag == "button")
            {
                //do button stuff
            }
            else
            {
                PlaceKitten(t.position);
            }
        }
    }
ryeMoss
  • 4,303
  • 1
  • 15
  • 34
  • Thank you, I added "button" tag to the button, but for some reason when I'm using this code I can't place an object at all, no matter if I click on the button or anywhere else on the screen. For testing I moved "PlaceKitten" function there where You have "do button stuff", but if I click the button nothing happens. – Rumata Jul 29 '17 at 14:28
1

I've answered this before but couldn't find the duplicate to close this question. When I do find it, I will close it.

You need to check if the mouse position is over the UI before considering that as a valid click.

This is done with the IsPointerOverGameObject function.

void Update()
{
    if (Input.touchCount == 1)
    {
        // Trigger place kitten function when single touch ended.
        Touch t = Input.GetTouch(0);
        if (t.phase == TouchPhase.Ended)
        {
            //Make sure we are not over the UI
            if (!UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
            {
                Debug.Log("Clicked outside the UI");
                PlaceKitten(t.position);
            }
        }
    }
}

For a touch screen devices, you have to pass the fingerId to the function:

void Update()
{
    if (Input.touchCount == 1)
    {
        // Trigger place kitten function when single touch ended.
        Touch t = Input.GetTouch(0);
        if (t.phase == TouchPhase.Ended)
        {
            //Make sure we are not over the UI
            if (!EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
            {
                Debug.Log("Touched outside the UI");
                PlaceKitten(t.position);
            }
        }
    }
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Thank you, but for some reason, it doesn't prevent me from placing objects when I'm clicking on the buttons. Should I change anything in the scene, maybe add any tags to the objects? At the moment my UI is in render mode "Screen space - overlay". – Rumata Jul 29 '17 at 14:30