I followed this tutorial and basically I did everything same. However I can’t reach methods inside the testTouch class. Here is TouchInput class:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class touchInput : MonoBehaviour { public LayerMask touchInputMask; private List<GameObject> touchList = new List<GameObject>(); private GameObject[] touchesOld; private RaycastHit hit; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { #if UNITY_EDITOR
//same things for unity editor as be shown in tutorial #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 recepient = hit.transform.gameObject; touchList.Add(recepient); if (touch.phase == TouchPhase.Began) { recepient.SendMessage("OnTouchDown", hit.point, SendMessageOptions.DontRequireReceiver); } if (touch.phase == TouchPhase.Ended) { recepient.SendMessage("OnTouchUp", hit.point, SendMessageOptions.DontRequireReceiver); } if (touch.phase == TouchPhase.Stationary) { recepient.SendMessage("OnTouchStay", hit.point, SendMessageOptions.DontRequireReceiver); } if (touch.phase == TouchPhase.Canceled) { recepient.SendMessage("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver); } } } foreach (GameObject g in touchesOld) { if (!touchList.Contains(g)) g.SendMessage("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver); } } } }
and here is testTouch class:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class testTouch : MonoBehaviour { public Color defaultColor; //set manually in the editor public Color selectedColor; //set manually in the editor public Material mat; void Start() { mat = this.GetComponent<SpriteRenderer>().material; // mat.color = selectedColor; } void OnTouchDown() { mat.color = selectedColor; Debug.Log("hello"); } void OnTouchUp() { mat.color = defaultColor; Debug.Log("hello"); } void OnTouchStay() { mat.color = selectedColor; Debug.Log("hello"); } void OnTouchExit() { mat.color = defaultColor; Debug.Log("hello"); }
I don’t get any errors and I can’t reach those Debug.Logs as well. When I click the 2Dcircle nothing happens.
I tried on editor and on my android phone. Both of them is not working.
Answer
3D Raycasting (Physics.Raycast()
) on a 2D colliders does not work.
Replace 2D circle collider with 3D circle collider.
There is no overload of the Raycast
method that takes a layerMask
as a third argument.
https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
You are mistakenly calling another overload of the Raycast
method.
public static bool Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);