- Home /
Question by
SynGameDev · Jun 29, 2020 at 09:14 AM ·
c#collisioncolliders
Detecting mouse over PNG
Hi All,
I have a basic maze PNG that i want to move a cube through, if the cube touches the side of the wall it fails the maze. I know that I can use collider to detect the collision, but I was wondering there is an easier way to detect collisions, over the colored part of the maze if that makes sense.
Comment
Yes. Knowing your position xy on the map you can check for collision data from pixel color:
[SerializeField] Texture2D map;
bool can_go_right = map.GetPixel( x+1 , y ) == Color.white;
bool can_go_left = map.GetPixel( x-1 , y ) = Color.white;
bool can_go_up = map.GetPixel( x , y+1 ) == Color.white;
bool can_go_down = map.GetPixel( x , y-1 ) == Color.white;
The texture must have the read/write enabled flag set in the texture import settings, otherwise this function will fail.
Also, it's a good idea to make this texture uncompressed as compression changes pixel colors slightly.