I tried converting this script:
public class TouchInput : MonoBehaviour {
public LayerMask touchInputMask;
private List<GameObject> touchList = new List<GameObject>();
private GameObject[] touchesOld;
private RaycastHit hit;
// Update is called once per frame
void Update () {
#if UNITY_EDITOR
if(Input.GetMouseButton(0) || Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(0)) {
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);
} if(Input.GetMouseButtonDown(0)) {
recipient.SendMessage("OnTouchUp", hit.point, SendMessageOptions.DontRequireReceiver);
} if(Input.GetMouseButtonUp(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
}
}
To 2d, where i got this script.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TouchInput : MonoBehaviour {
public LayerMask touchInputMask;
private List<GameObject> touchList = new List<GameObject>();
private GameObject[] touchesOld;
public RaycastHit hit;
public GameObject recipient;
// Update is called once per frame
void Update () {
#if UNITY_EDITOR
if(Input.GetMouseButton(0) || Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(0)) {
touchesOld = new GameObject[touchList.Count];
touchList.CopyTo(touchesOld);
touchList.Clear();
//Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
if (Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero)) {
recipient = hit.transform.gameObject;
touchList.Add(recipient);
if(Input.GetMouseButtonDown(0)) {
recipient.SendMessage("OnTouchDown", hit.point, SendMessageOptions.DontRequireReceiver);
} if(Input.GetMouseButtonDown(0)) {
recipient.SendMessage("OnTouchUp", hit.point, SendMessageOptions.DontRequireReceiver);
} if(Input.GetMouseButtonUp(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
}
}
i Changed if(Physics.Raycast(ray,out hit,touchInputMask)) { to if(Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero)) {
and deleted the ray variable.
I change the layer of my touch object to touchInputMask, and attach a script that says: void OnTouchDown () {
It works fine unity i play, and press on the 2d object (Which is a moving object, and when i tap it it needs to be destroyed) and it then says “cant find instance of object” something like that.
Its because it doesn’t know what the variable “hit” is… How do i change the script to 2d, so it knows what hit is?