M
M
Matvey Nazarov2022-02-27 21:08:59
Unity
Matvey Nazarov, 2022-02-27 21:08:59

How to track clicks on an object in Unity for mobile?

Hello users of Habr! The problem is that I can neither find nor think of a way to track when an object is touched across the screen. At first I used the OnMouseDown() method, but when compiling to apk, the engine complained about this.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GFX Data, 2022-02-27
@Matvey635

The object needs a collider and the following script:

using UnityEngine;
using UnityEngine.EventSystems;

public class PointerListener : MonoBehaviour, IPointerDownHandler
{
    public void OnPointerDown(PointerEventData eventData)
    {
          // Тут обрабатываем нажатие тачем или мышкой
    }
}

F
freeExec, 2022-02-27
@freeExec

Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
if (Physics.Raycast(ray))
{}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question