- Home /
Question by
Rubesom · Jun 14, 2014 at 07:38 PM ·
touchscreen
Touch Input Mask
Hi
I have code:`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;
private RaycastHit hit;
void Update () {
if (Input.GetMouseButton(0) || Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(0)) {
touchesOld = new GameObject[touchList.Count];
touchList.CopyTo(touchesOld);
touchList.Clear();
Ray ray = 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.GetMouseButtonUp(0)) {
recipient.SendMessage("OnTouchUp",hit.point,SendMessageOptions.DontRequireReceiver);
}
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 = 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);
}
if (touch.phase == TouchPhase.Ended) {
recipient.SendMessage("OnTouchUp",hit.point,SendMessageOptions.DontRequireReceiver);
}
if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved) {
recipient.SendMessage("OnTouchStay",hit.point,SendMessageOptions.DontRequireReceiver);
}
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);
}
}
}
}
} ` And I get this error
UnityEngine.Physics:Raycast(Ray, RaycastHit&, Single) TouchInput:Update() (at Assets/TouchInput.cs:27) I use this tutorial:[here][1] and I can't put "Touch input" in Touch Input Mask inside Main Camera in script. [1]: https://www.youtube.com/watch?v=SrCUO46jcxkScene::raycastClosestShape: The maximum distance must be greater than zero!
Comment
Your answer

Follow this Question
Related Questions
A node in a childnode? 1 Answer
Android Input 1 Answer
A beginner's questions!!! 4 Answers
Unity remote cant work 0 Answers
To hide the Touchscreen keyboard ios 0 Answers