Regarding actor movement and Root Components

In most of the tutorials I have seen, an empty scene object is used as the Root Component of many actors. I am wondering how should I correctly be moving my actors if my other components are attached to the root, this is my current hierarchy:

if (!RootComponent)
{
	RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("Root Component"));
}

PaddleMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Paddle Static Mesh"));
PaddleMeshComponent->SetupAttachment(RootComponent);

I have a static mesh which is attached to an empty scene object. I am adding forces to this actor like so:

PaddleMeshComponent->AddImpulse(CurrentVelocity, NAME_None, true);

Is this correct?

The reason I ask is because I am currently trying to get a ball actor to follow a paddle actor before the ball is launched, but the ball remains in place and does not move with the following code:

if (!bIsLaunched)
{
	BallMeshComponent->SetWorldLocation(Paddle->GetTargetLocation());
}

However, the ball will follow the paddle if I remove the empty scene component from the paddle and set the static mesh component as the root like so:

PaddleMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Paddle Static Mesh"));
RootComponent = PaddleMeshComponent;

I feel like something I’m doing wrong is incorrect, am I correctly setting Root Components? Do I have my movement logic wrong? Any insight would be appreciated.

1 Like

I’ve read that the best practice is to make a scene component as root, just as a container to hold all other components. Would you recommend setting the static mesh component as the root?

Why do you need a scene component as root? What are you using it for?

Well just speaking from looking at actual assets from epic, they themselves don’t use scene components as root as far as I’ve seen.

A good point was made by Crowley Caine, after checking some of the pre-delivered templates, Epic don’t appear to use the empty Scene Component as the root.

But I believe the issue I had here was that I was setting the BallMeshComponent Location to the RootComponent of the Paddle, but the paddle movement is done on the PaddleMeshComponent and not the Root, so the code was behaving as expected.

What I should have been doing was setting the ball movement to the PaddleMeshComponent (as this was the actor that moves, not the root) so by changing the code from

BallMeshComponent->SetWorldLocation(Paddle->GetTargetLocation()); // uses the root of the paddle

to

BallMeshComponent->SetWorldLocation(Paddle->PaddleMeshComponent->GetComponentLocation()); //uses the paddle mesh component

The ball now moves with the paddle as expected.