How do you detect if a touch is hitting an object?
Hi,
i have a 2d project that is running on android (via unity remote 5)
and i'm trying to detect when the player is touching an object.
The following is my code:
void Update()
{
foreach(Touch touch in Input.touches)
{
if(touch.phase == TouchPhase.Began)
{
Debug.Log("touch");
RaycastHit2D hit = Physics2D.Raycast(touch.position, Vector2.zero, Mathf.Infinity);
if (hit.collider)
Debug.Log("hit");
}
}
}
The problem is that it never prints "hit" - even when im actually pressing an object.
The only problem i can see with the code is that Physics2D.Raycast takes in a vector2d
as the direction, but the direction it should raycast in is in the z direction.
But i heard you could get around that if you passed in a zero vector (which is what im doing) but it still doesn't seem to work.
I would really appreciate it if somebody could tell me why it isn't working.
Thanks in advance!
Comment