- Home /
Rotate Child with parent, keeping Child axis in the original direction
Hey guys, I've been struggling with this problem for a couple of days now. I've got two cylinders (child objects) on each end of a stick (parent), when I rotate the stick, I want the cylinders to move with the stick, but I also want them to remain facing up. I've tried using transform.localRotation, EulerAngles, lookAt() you name it, nothing works. I figured it's because the child inherits the parent's rotation.
Also important to note is that I'm not using rigidbodies Any help would be greatly appreciated! Thanks
Answer by oStaiko · Sep 21, 2020 at 11:00 PM
You have several options here, depending on the behavior you want. If you want them to be free-moving physics bodies, then attach a Rigidbody on both of the cylinder, and connect them to the bar using a Hinge Joint component. This will let them swing a bit as the bar moves.
If you want them to always stay perfectly vertical, you can either: Unparent them and attach a script that makes them follow the position of the bar tips without changing their rotation - or keep them parented, and attach a script to them that changes their rotation to Quaternion.identity at the end of every frame.
I'd also recommend reworking their hierarchy a bit too. Create two empty objects to represent the tips of the bar where you want to the cylinders to be 'attached to', and have your scripts follow that transform.
Answer by SlowCircuit · Sep 21, 2020 at 11:49 PM
If you want them to move with the stick but stay in their original rotation, then you would set their rotation not their local rotation.