Question by
dfeldhaus · May 09, 2017 at 05:48 AM ·
transform.positionchildrentransform.rotationquaternionsattachment
How to attatch two objects at their children, with child rotation
Okay, so this is kind of difficult to explain, but basically I've got this problem: I want to be able to attach two objects (shown in black below) using one of several attachment points (shown in green below). My drawing is 2D, but I'm trying to do it in 3D. Once they're attached, I then need to be able to rotate the attached object around that axis.
So far, I have the objects and the attachment points, and the attachment points are rotated based on where they are on the object (so the one on the left would be 180 deg, and the one on the right would be 90).
I wrote some code, but it's real hacky, it requires directly manipulating the Transforms, and you can't rotate around the connection axis.
//attaching = attachment point of attaching object
//attachTo = attachment point of object being attached to
public void MoveToAttachmentPos(Transform attaching, Transform attachTo){
Quaternion originalRot = attachTo.parent.rotation;
attachTo.parent.rotation = Quaternion.Inverse (attachTo.localRotation);
attaching.parent.rotation = Quaternion.Inverse (attaching.localRotation);
//Rotate child parent so that it's facing down
attaching.parent.Rotate(0,0,180,Space.World);
attaching.parent.position = attachTo.position - (attaching.position - attaching.parent.position);
attaching.parent.parent = attachTo;
attachTo.parent.rotation = originalRot;
}
Here's another example:
veryclear.png
(11.5 kB)
part2.png
(98.2 kB)
Comment