- Home /
Touchinput script converted from 3d to 2d
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?
In your another question, I suggested you to check the documentation of raycast2d and catching hit. You are asking same question again!
Answer by hexagonius · Dec 08, 2016 at 06:17 PM
The Physics2D.Raycast does not return a bool like the 3D version does, but the RaycastHit2D directly. If you look at the documentation here there are 2 important lines:
RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up);
if (hit.collider != null) {...
adapt your version with this and you should have your "hit" again.
Your answer
![](https://koobas.hobune.stream/wayback/20220612102318im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Move horizontally on touch 0 Answers
Ui sidescroller 2d touch ? 0 Answers
How to detect a touch on a 2D sprite 2 Answers
Simultaneous Touch Drag Controls 0 Answers