Child actor does not follow parrent unless it selected in editor

When I’m attaching actor to actor at runtime, child don’t follow parent rotation/location unless I’ve selected it in world outliner. As soon as I’ve selected, it works just fine. The point is that the parent acrot should work just as an anchor, other actors attaches dynamically.

AttachRootComponentToActor(blkCtrl->building);//Attaches actor

Unreal Engine 4.7.2

Hey -

Are you referring to attaching an actor to another actor or are you creating an child actor component and attaching it? Could you explain how you’re attaching the child and parent together? Are the parent and child both code based?

An actor to another actor. Yes, they both code based. The main thing that, it works fine after you selected each object in world outliner. It looks like this, “Ancor” actor exist in the scene and rotates, then we create some another actor, lets call it “block”, and attaching it using “AttachRootComponentToActor()” from its class. So in hierarchy it’s attached, but it does not behave like child of Anchor. I mean, it doesn’t rotates with Anchor. And if I select it in world outliner it’s gonna behave the way it should. It’s gonna rotate with parent. So I don’t now whether it’s bug or I’m doing something wrong, because I’m quite new to unreal engine. I tried to enable tick function in Anchor children, but the result is still the same. My guess was that, maybe those children actors don’t update transform every frame.

Hey -

Using AttachRootComponentToActor() will only attach the root component of the child actor to the root component of the parent. To have the Child Actor follow the Parent Actor’s movement/rotation/scale in game you want to add a call to:

SetActorTransform(Parent->GetTransform());

where “Parent” is a pointer variable to the parent class. Doing this in the Tick() function of the child will update it’s transform every frame to match its parent. You can also use the following for additional control if you need to offset the child and parent in game:

SetActorLocation(Parent->GetActorLocation() + FVector(offsetX , offsetY , offsetZ));
SetActorRotation(Parent->GetActorRotation() + FRotator(offsetX , offsetY , offsetZ));
SetActorScale3D(Parent->GetActorScale() + FVector(offsetX , offsetY , offsetZ));

Thanks, I’ll try that. The thing is, child actor follow the Parent actor’s, but only when it has been selected in the world outliner.

Are you sure you changed the transformation of the RootComponent? The documentation says:

Actors support having a hierarchy of SceneComponents. Each Actor also has a RootComponent property that designates which Component acts as the root for the Actor. Actors themselves do not have transforms, and thus do not have locations, rotations, or scales. Instead, they rely on the transforms of their Components; more specifically, their root Component. If this Component is a SceneComponent, it provides the transformation information for the Actor. Otherwise, the Actor will have no transform. Other attached Components have a transform relative to the Component they are attached to.

If you have a lot of components attached to an actor - you might have different SceneComponentes and you did not change the transformation of the RootComponent but of an Child component (which is a relative transformation to its parent).

RootComponent

  • SceneComponent1

------> StaticMeshComponent1

  • SceneComponent2

------> StaticMeshComponent2

If you change the transformation of the SceneComponent1 it will have no effect on the SceneComponent2 (and no further effect on the childs of the SceneComponent2). Make sure you really selected the “RootComponent”.

I achieved needed result. But I can’t tell how I did that (it’s been a while). Now it’s working like this - UStaticMeshComponent->AttachTo(AActor->GetRootCompoent())
The weird thing was that it didn’t follow parent rotation unless I’ve selected an actor in world outliner.

You don’t want to update the position of your actor at each frame, this is very heavy, inefficient and bypass the concept of parenting which exists to prevent doing this by applying each transform of the parenting chain properly.