Get localScale depending on rotation

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

  1. Move your cursor onto the three available faces of the solid cube (Object B).
  2. Notice even if Object A turns red, it will still be perfectly aligned with Object B
  3. Press Shift + Q
  4. Repeat Step 1
  5. Notice that Object A will now clip into/move away from Object B as rotation has not been considered.

Hey there,

You can use Transform.InverseTransformDirection to get the scale into the local sizes, something like:

var norm = hit.normal;
norm = norm.normalized;
var locScale = CubeGhost.transform.InverseTransformDirection(CubeGhost.transform.localScale*0.5);
locScale = Vector3(Mathf.Abs(locScale.x), Mathf.Abs(locScale.y), Mathf.Abs(locScale.z));
var off = Vector3.Scale(locScale, norm);
norm = Vector3.Scale(norm, norm);

var pos = hit.transform.position + Vector3.Scale(hit.point-hit.transform.position, norm);
CubeGhost.transform.position = pos + off;

That should position your object based on both objects rotations.

Nice skybox btw!

EDIT:

You can also do this with collider bounds as I suggested in a post on your last question, this is also less likely to fail if you start using parents and children where CubeGhost.transform.localScale can be incorrect unless you take the parent into account.

var norm = hit.normal;
norm = norm.normalized;

var min = CubeGhost.collider.bounds.min;
var max = CubeGhost.collider.bounds.max;
var off = norm == Vector3.Scale(norm, norm).normalized ? min : max;
off -= CubeGhost.transform.position;
norm = Vector3.Scale(norm, norm).normalized;
off = Vector3.Scale(off, norm);

var pos = hit.transform.position + Vector3.Scale(hit.point-hit.transform.position, norm);
CubeGhost.transform.position = pos - off;

Scribe