0

In this game there are many object that has the tag "enemy" all these enemy are instances of the same class, in this class I used the method "OnMouseDown" to handle the user's click but this is not the perfect way because it is not compatible with the multitouch

I looked around on the net and I found this solution:

public class TouchInput : MonoBehaviour {

public LayerMask touchInputMask;

private List<GameObject> touchList = new List<GameObject>();
private GameObject[] touchesOld;
private RaycastHit hit;

void update()
{
#if UNITY_EDITOR
    if (Input.GetMouseButton(1) || Input.GetMouseButtonUp(1))
    {
        touchesOld = new GameObject[touchList.Count];
        touchList.CopyTo(touchesOld);
        touchList.Clear();

            Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out hit, touchInputMask))
            {
                GameObject recipient = hit.transform.gameObject;
                touchList.Add(recipient);

                if (Input.GetMouseButtonDown(0))
                {
                    recipient.SendMessage("OnTouchDown", hit.point, SendMessageOptions.DontRequireReceiver);
                }
                else if (Input.GetMouseButtonUp(0))
                {
                    recipient.SendMessage("OnTouchUp", hit.point, SendMessageOptions.DontRequireReceiver);
                }
                else if (Input.GetMouseButton(0))
                {
                    recipient.SendMessage("OnTouchStay", hit.point, SendMessageOptions.DontRequireReceiver);
                }
            }
        }

        foreach (GameObject g in touchesOld)
        {
            if (!touchList.Contains(g))
            {
                g.SendMessage("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
            }

        }
 #endif


    if (Input.touchCount > 0)
    {
        touchesOld = new GameObject[touchList.Count];
        touchList.CopyTo(touchesOld);
        touchList.Clear();

        foreach (Touch touch in Input.touches)
        {
            Ray ray = GetComponent<Camera>().ScreenPointToRay(touch.position);

            if(Physics.Raycast(ray, out hit, touchInputMask))
            {
                GameObject recipient = hit.transform.gameObject;
                touchList.Add(recipient);

                if(touch.phase == TouchPhase.Began)
                {
                    recipient.SendMessage("OnTouchDown", hit.point, SendMessageOptions.DontRequireReceiver);
                }
                else if (touch.phase == TouchPhase.Ended)
                {
                    recipient.SendMessage("OnTouchUp", hit.point, SendMessageOptions.DontRequireReceiver);
                }
                else if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved)
                {
                    recipient.SendMessage("OnTouchStay", hit.point, SendMessageOptions.DontRequireReceiver);
                }
                else if (touch.phase == TouchPhase.Canceled)
                {
                    recipient.SendMessage("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
                }
            }
        }

        foreach (GameObject g in touchesOld)
        {
            if (!touchList.Contains(g))
            {
                g.SendMessage("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
            }
        }
    }
}

}

But I think that this solution does not work in a 2d game (I tried it, I attached this script to the main camera, there aren't any errors, but the method "update" seems to not be never called for some reasons).

Any Ideas?

UPDATE:

The code I'm using:

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

public class TouchInput : MonoBehaviour, IPointerDownHandler
{

    void Start()
    {
        addPhysics2DRaycaster();
    }

    void addPhysics2DRaycaster()
    {
        Physics2DRaycaster physicsRaycaster = GameObject.FindObjectOfType<Physics2DRaycaster>();
        if (physicsRaycaster == null)
        {
            Camera.main.gameObject.AddComponent<Physics2DRaycaster>();
        }
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name);
    }

    //Implement Other Events from Method 1

 }

Main camera: main camera

EventSystem: EventSystem

Console: (there is only the unity ads advert) console

Hierarchy: hierarchy

My object: Coin object object components

Jr Antonio
  • 193
  • 1
  • 4
  • 20
  • EventSystems should be use. Its `PointerEventData.pointerId` property from one of its functions is used to handle multiple touch. See duplicate – Programmer Aug 28 '18 at 18:03
  • Hi @Programmer. EventSystem already was into the hierarchy... I tried the solution you posted under the post that you have just sent to me but it doesn't work. I have created this script https://pastebin.com/D9ZENc52 and I attached it to the 2d gameobject I would like to click but it doesn't work, am I missing something? – Jr Antonio Aug 28 '18 at 18:26
  • How do you display the 2D Image? A screesnhot of the component used? – Programmer Aug 28 '18 at 18:55
  • @Programmer it is a simple sprite: https://i.imgur.com/cAKE6LK.png – Jr Antonio Aug 29 '18 at 09:39
  • Which could be the problem? – Jr Antonio Aug 29 '18 at 09:50
  • 1.Coin object is not a UI object since it's using SpriteRenderer. Move the coin Object away from the Canvas. It's not a UI Object. Only UI objects can be child of Canvas. 2.Once you do so, read the **Troubleshooting the EventSystem** section from the duplicate. You are likely missing an EventSysyem. The duplicate shows how to create one. – Programmer Aug 29 '18 at 14:30
  • @Programmer I've done all you said but it still doesn't work.. – Jr Antonio Aug 29 '18 at 15:54
  • @Programmer who would call "OnPointerDown"? I've just read the documentation of the EventSystem and this method seems to not exist: https://docs.unity3d.com/ScriptReference/EventSystems.EventSystem.html – Jr Antonio Aug 29 '18 at 16:00
  • I've just found it: https://docs.unity3d.com/ScriptReference/EventSystems.IPointerDownHandler.html I don't know what to do... :/ – Jr Antonio Aug 29 '18 at 16:04
  • Unity's EventSystem will call it because you implemented `IPointerDownHandler`. 1.Click "Play". 2.Click on Coin object several times from the Game view. 3.Select the camera and post "Inspector" tab screenshot of it. 4.Select the EventSystem GameObject and post screenshot of it. 5.Post screenshot of the Console tab. 6.Post the current code you're using. Copy and paste it. It should be a simple click detection code. – Programmer Aug 29 '18 at 16:06
  • @Programmer I've just updated the post! – Jr Antonio Aug 29 '18 at 16:17
  • But you forgot to coin object. I also need to see the Inspector of it. So far nothing wrong in your project – Programmer Aug 29 '18 at 16:33
  • @Programmer any ideas? – Jr Antonio Aug 29 '18 at 18:26
  • Ok Just saw this now. It's very likely the scale of the coin – Programmer Aug 29 '18 at 18:26
  • X, Y scale should also be 1. Don't make it anything less than 1 or physics raycast made with EventSystem may not be able to work. Fix this and let me know if it's working – Programmer Aug 29 '18 at 18:26
  • https://i.imgur.com/Yy7cKFf.png it still doesn't work.. (Why the scale could be a problem?) – Jr Antonio Aug 29 '18 at 18:28
  • Zip the project, upload it somewhere and send me the link. I will run it and tell you the issue. The scale issue based on math Unity uses for their raycaset with eventsystem – Programmer Aug 29 '18 at 18:29
  • @Programmer : https://drive.google.com/open?id=1ufsgX3D84gPCzkihL1gPC0LQIS4CNuyo – Jr Antonio Aug 29 '18 at 18:43
  • You used a Rigidbody2D on your so you must enable "Simulated" on the Rigidbody2D or remove the Rigidbody2D component. This should fix your problem. My other advice for you to to resize the collider of the coin to be smaller(Not the coin object. The collider). It is way bigger than the coin. You can do that from the scene view. Google "how to resize a collider in Unity" . – Programmer Aug 29 '18 at 19:07
  • 1
    @Programmer Thank you very much! You always are very helpful – Jr Antonio Aug 30 '18 at 11:02

0 Answers0