Download

How to attach components maintaining their orientation

I’m trying to attach a component to another, and I would like the attached component to always keep its world orientation.

So, if the parent object rotates, I want the attached component to stay exactly where it was, but at the same time I want it to follow its parent if it moves around.

I tried doing it like this:



FAttachmentTransformRules rules(EAttachmentRule::SnapToTarget, EAttachmentRule::KeepWorld, EAttachmentRule::SnapToTarget, false);

childComponent->AttachToComponent(parent, rules);


Initially it works, meaning that when I attach the child it keeps its orientation. But as soon as the parent object rotates, the child also rotates, which is what I don’t want. How could I do it?

Instead of KeepWorld for the Rotation rule, I think you want KeepRelative.

Actually I tried that already, but it doesn’t work. As soon as I attach the child its orientation becomes the parent’s, and if the parent rotates so does the child. With KeepWorld at least when I attach the child its orientation remains the same; the problem is that it doesn’t when the parent rotates.

Hmmm, You could also just calculate the Positional Deltas every frame from your parent and apply those manually in your child’s Tick method. Not the most elegant of solutions (I agree that this seems like something Attach To Component should just handle), but it would work.

That’s actually how I’m handling it for now, but like you said, it’s not elegant at all. There is probably a way with AttachToComponent, it’s just that I’m missing it :confused:

Hi, you might want to refer to this: