Hi,
I have a game in which there is a picture in Colo32[ ] format. It stores a figure and has a transparent background. The figure constantly changes, so I need to change the collider. Does anyone have any idea how to make a box/mesh/plane collider out of such a picture in the code?
Here is a picture with my thoughts:
Thank you in advance 
Ideally you would only create the collider once and just update it’s bounds. Color32[ ] is just an array… all you need is to use a little math to convert that to a 2D array (don’t actually convert the array, just do it as you process it).
So, let’s say you have an image that’s 100 x 100 as an easy example. Your Color32[ ] array would have 10,000 elements in it. You’re going to have to process all of those elements… you just do a little math to figure out which “row” and “column” you are in. And since you’re using transparency, just check the alpha channel to find the first values greater than 0 (zero). You’ll need to find:
A) The index of the topmost row with a pixel alpha > 0.
B) The index of the “leftmost” column with a pixel alpha > 0.
C) The index of the “rightmost” column with a pixel alpha > 0.
D) The index of the bottommost row with a pixel alpha > 0.
So first you need to know the size of your object so you can calculate what each pixel translates to in distance. Then:
rightmost index - leftmost index will give you the bounding width in pixels
bottommpost index - topmost index will give you the bounding height in pixels
So, if you know where the center is for your object… shift your bounds over by (leftmost index * pixelDistaince) and shift it down by (topmost index * pixelDistance). Then set the width as the bounding width in pixels * pixelDistance and do the same for the height. You can replicate these steps for depth as well.
Thank you!!! That works for the box collider.
Do you think it is possible to make a mesh collider out of the image?
Like this:
Yes but it would be a lot more complex. You’d have to figure out where all of the triangles are going to be. Since you’re dealing with a 2D shape, as long as your shape doesn’t ever get super complex you would just need to find the edges of your shape. You would essentially do the same steps as above (finding the left, right, top and bottom most points) and keep finding points… calculate the slope (angle) between your found pixel and the next… as long as that angle doesn’t change, just keep going… once you hit a point where the angle changes you consider that a new point and then start the process over for that point.
To do it efficiently so it wouldn’t require multiple passes through the array would start to get fairly complicated. Once you found all of your points, you would project those (essentially duplicating the shape) whatever distance along the Z axis. Then you would need to connect the two shapes with triangles to form the side faces.
Additionally, since you’d be dealing with an image you would one some degree of variation allowed to make sure you’re making smooth lines just in case you have some antialiasing that would cause unnecessary points to be generated.
How many possible shapes do you anticipate there being?
Thank you. I will have a lot of different shapes. Like it will be changing a lot of times, so I cannot make all colliders in Blender or something. I see your idea. Do you have any example where someone makes a mesh collider in code (because I had an experience of just dealing with the BoxCollider in code).
Unfortunately I don’t but I’m sure someone does. I just understand the concepts behind it.
Cool. Thanks!
Anyone has such an example? Please 
The UCLA Mesh Creator might help you with your problem
Or you could collide manually with the pixels…