This is a folow up question to an answer given to me in the following thread - Calculate distance between any objects with unknown dimensions - Unity Answers
What I’m currently able to do with my game is align Object A (Transparent cube) so it’s right next to Object B (Solid cube) regardless of the scale of Object A, however, this doesn’t work if you rotate Object A. I don’t know how to add rotation to my current calculation. Here’s the code I’m using
//Move cubeghost next to buildingblock we hit
CubeGhost.transform.position = hit.transform.position + hit.normal;
//Move cubeghost depending on scale of buildingblock we hit
CubeGhost.transform.position -= Vector3.Scale(CubeGhost.transform.TransformPoint(hit.transform.position), hit.transform.localScale / 2);
//Move cubeghost depending on scale of cubeghost
CubeGhost.transform.position += Vector3.Scale(CubeGhost.transform.TransformPoint(hit.transform.position), CubeGhost.transform.localScale);
I’m using Vector3.Scale to get the correct axis I’m working with. So instead of returning (1, 0.5, 0.3) it will return (0, 0, 0.3) if I’m working with the Z axis.
If you still aren’t quite sure what the issue is, feel free to try it out for yourself here: https://dl.dropboxusercontent.com/u/40950257/VoxelSpaceThingy/VoxelSpaceThingy.html
Here are some steps you can follow to get a visual example
- Move your cursor onto the three available faces of the solid cube (Object B).
- Notice even if Object A turns red, it will still be perfectly aligned with Object B
- Press Shift + Q
- Repeat Step 1
- Notice that Object A will now clip into/move away from Object B as rotation has not been considered.