Hey Guys, This is my problem…

I’m building a puzzle game where the distance between each piece in the puzzle is crucial, they need to be aligned for the game to run correctly.

For instance… I have an exact distance of .25 between my pieces, I would like to be able to check if the distance is higher than that in the my game logic but I get unprecise numbers with several digital places when I call a Vector3.Distance or I print out their position…

So far I have fixed this adding a small margin of .01 when I check the distance in an “if” statement. I would like to know if there is a better way or maybe a different way to deal with this problem.

Many Thanks.

I think we’d need to know a bit more about the game mechanics to recommend a specific solution (I would, at least). Generally speaking though, if the game is strictly grid-based, then using a simple 2-d array would be the most straightforward solution. If the game isn’t grid based (for example, these are physics objects and you’re trying to determine when they’ve formed a certain configuration), then some sort of heuristic will be needed to determine when you’ve gotten ‘close enough’ to the desired configuration.

(There may be other suitable solutions as well, depending.)

A common way to handle puzzle games, more specifically match-3 types, is to let the logic be handled by some sort of array or list structure, then only reflect the changes as needed graphically. So, for instance, by having a 2D array or list, you could successfully program the logic for something like tetris with math, instead of messing with the individual colliders.

I’m not sure if this would work for your situation; can you provide more details about how it’s set up?

If its feasible, I’d suggest increasing your game world scale by 4, leaving an exact distance of 1 unit between your pieces. You can then convert any decimals to integers to rid yourself of any decimal places and minor inaccuracies.