- Home /
Getting the screen space bounding box of a sprite
I have sprites that are supposed to be tapped, and to make it more efficient I'd rather not use raycasts. So I was thinking of getting the screen space coordinates of the sprite's bounding box and then just checking if the touch was inside the box.
Question is, how do I go about getting that bounding box?
hi @srylain - if you google "unity sprite bounding box" you'll get to this page: https://docs.unity3d.com/2018.2/Documentation/ScriptReference/Sprite-bounds.html
Answer by eses · Aug 13, 2018 at 10:20 AM
hi @srylain - if you google "unity sprite bounding box" you'll get to this page: https://docs.unity3d.com/2018.2/Documentation/ScriptReference/Sprite-bounds.html This will get you the bounds in world space. But this is not enough - you'll need to convert world space positions to camera space or actually screen space, there is Camera.main.WorldToScreenPoint for this. You can verify how it works, by testing it first using your sprite's transform position. Then you can find out position of each sprite bounds corner using it. This can be done using min and max values of bounds.