Hello, I’ve a 2D world with camera at z=-20, canva and some gameobjects. I’m trying to get the GameObject I did hit and the position of the hit, but it’s not working, I always get coord 0 0 and it says I hit no target, can someone help ? Thanks in advance
if (Input.touchCount > 0)
{
Touch getTouch = Input.GetTouch(0);
if (getTouch.phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay(getTouch.position);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
{
if (hit.collider.gameObject == this.gameObject)
{
selected = true;
this.transform.localPosition = hit.point;
Debug.Log(hit.point);
}
}
}
}
In fact it worked when my GameObject where in the canvas, but it’s not convenient. The Debug.Log(hit.collider.gameObject); do not even show anything and when I debug.draw the raycast there is nothing. If you have any code I can find on the internet to get some inspiration, I take