Rotation Changing Z-Coordinate Value
Hello! Sorry, this is a really strange and complicated question, so be prepared for a bit of a confusing read! If you don't want to read my tale of woe, and just want to skip down to the problem, scroll down to the bolded text!
I've been working on a silly little mini-golf game (since they're all the rage these days) for some friends and I to play when we're bored. I'm working on a level editor, which has been going quite well! But I've recently hit a little issue.
Each "track piece" for the mini-golf course is about 1 Unity-unit big, with the exception of multi-sized pieces (E.g. pieces that are long may take up 2 Unity-units). My initial idea was to make each unity-unit that the piece takes up contain a "node" that's basically just an empty game object with a trigger box collider.
In order to detect whether or not a track piece occupies the same space, I'd have a dictionary of Vector3 keys and TrackPiece (a class containing information about each track piece, such as a list of it's nodes) values. When a piece was placed down, I would go through each node's position and add it to the dictionary. Then, when placing down a new piece, I would check to see if any of node positions of the new piece were already in the dictionary.
This was working excellently, until I started to add rotation (in increments of 90 degrees). Checking if the dictionary contained a key for each node stopped working, allowing overlap on 2-unit-big track pieces. I was utterly baffled! I didn't change the position in my code. It suddenly would allow overlap if the rotation was anything other than 0.
After much testing and changing the dictionary to use a string key (where I would take the nodes' vector3s and turn them into strings), I realized that rotating around the y-axis would alter one of the nodes' z-coordinates by an infinitely small number (e.g. -0.00000000000000001). It would turn back to normal when I rotated the parent object back to 0 degrees.
(I called the nodes "cells" in my code, as you can see on the right in the inspector -- just wanted to avoid any confusion)
In the picture above, I have the code print out the string-vectors of each node upon placement. As you can see, the nodes of the first piece were (-2,0,1) and (-1,0,1). The nodes of the second are (-3,0,1) and (-2,0,0.99999...) (which should be (-2,0,1), indicating an overlap).
I have a temporary fix -- I just round the z-coordinate whenever I convert the vectors to strings. However, this is really bugging me. I'd like to find a better fix for this, since converting the vectors to strings is a lot less clean than simply using the vector3s as keys to begin with.
Does anyone know why Unity would do this? Is this a bug with Unity, or do I just not know how rotation works?
Thanks so much!