- Home /
Move Parent Along Childs Local Axis
I'm building a 3D puzzle in which the camera rotates around the puzzle on a central axis. I want to be able to move the pieces relevant to the camera direction only on X and Y.
The piece has a plane within it as a child. This plane is always facing the camera, this plane is what detects the Raycast. I want to be able to move the piece along the plane axis on X and Y.
Below as an image of the piece selected (I'm looking through the game camera), and as you can see, the axis is incorrect for the movement of X and Y relevant to the camera. The purple plane is the plane I mentioned above, it always faces the camera.
As you can see, when I select the plane, the axis is correct. So I need to somehow move the piece (plane's parent), along this axis only on X and Y.
How can I achieve this? The raycast update is called like this on the puzzle piece:
//hitPoint is the location of the raycast on PLANE
function UpdatePiecePosition(hitPoint : Vector3){
//right now - I'm just doing a general position
//raycast plane is moved as well as its a child - this is fine
transform.position = hitPoint;
//update planes rotation to camera
plane.transform.LookAt(cam.transform);
}
Answer by DiegoSLTS · Oct 15, 2015 at 01:32 PM
Check the Transform.TransformDirection and Transform.InverseTransformDirection methods. You can get, say, the transform.right of the plane (which returns the right direction in local space):
Vector3 right = plane.transform.right;
... trasform that direction into a world space direction:
right = plane.transform.TransformDirection(right);
... and then transform that direction into the local space of the parent game object:
right = theParentObject.transform.InverseTransformDirection(right);
You can now move the object in that direction.
Thanks. Why would I want the right direction though? The direction of the plane changes depending on the axis of the camera that rotates around the 3D puzzle.