2D How to Detect if sprite is touched ?
i try to make the game for android but first i need to make the buttons they are sprites and how to detect if they are touched ?
i write on C#
Hello Merhat,
To detect when the user touch the sprite you would need to:
- Get touch position
- Convert that touch position from screen to world point
- Then use Physics2D.OverlapPoint to check if your touching any collider( so you need to have 2d collider attached to the sprite gameobject)
Final code:
Vector3 worldPoint = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
Vector2 touchPos = new Vector2(worldPoint.x, worldPoint.y);
if (col == Physics2D.OverlapPoint(touchPos)){
}
2 Likes