void Update()
{
for ( int i = 0; i<Input.touchCount; i++)
{
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
Touch touch = Input.GetTouch(i);
Vector3 touchposition = Camera.main.ScreenToWorldPoint(touch.position);
RaycastHit2D hit = Physics2D.Raycast(touchposition,touchposition);
Debug.DrawRay(touchposition, touchposition);
if (hit.collider)
{
Debug.Log(hit.transform.name);
}
}
}
}
Finally I got it working :
void Update()
{
foreach (Touch touch in Input.touches)
{
//Detects Swipe while a finger touched the screen
if (touch.phase == TouchPhase.Began)
{
Ray touchposition = Camera.main.ScreenPointToRay(new Vector3(touch.position.x, touch.position.y, 0f));
RaycastHit2D hit = Physics2D.GetRayIntersection(touchposition);
if (hit.collider)
{
Debug.Log(hit.transform.name);
}
}
do it in foreach:
foreach (Touch touch in Input.touches)
{
//Detects Swipe while a finger touched the screen
if (touch.phase == TouchPhase.Began)
{
}
//Detects Swipe while finger is still moving
if (touch.phase == TouchPhase.Moved)
{
}
//Detects swipe after finger is released
if (touch.phase == TouchPhase.Ended)
{
}
}
wait are you sure it is supposed to be in Update() instead of fixedUpdate?