Download

Child Components Spawning At Origin

I’m working on a really basic pawn actor with a box collision as the root component instead of a capsule component. I want to try my hand at some custom physics and movement components, but that’s neither here nor there. The issue I’m running into is any components I’m adding through code are always spawning at the origin of the world even though I attach them to the root UBoxComponent.



APAWN_2DCharacter::APAWN_2DCharacter(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;

collisionBox = ObjectInitializer.CreateDefaultSubobject<UBoxComponent>(this, TEXT("Collision Box"));
collisionBox->SetBoxExtent(FVector(100, 100, 100));
collisionBox->SetCollisionProfileName(FName("Pawn"));
collisionBox->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);

hitBox = ObjectInitializer.CreateDefaultSubobject<UBoxComponent>(this, TEXT("Hit Box"));
hitBox->SetBoxExtent(FVector(100, 100, 100));
hitBox->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);

staticMesh = ObjectInitializer.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("Static Mesh"));
static ConstructorHelpers::FObjectFinder<UStaticMesh>PlaneMesh(TEXT("/Engine/BasicShapes/Plane.Plane"));
UStaticMesh* PlaneAsset = PlaneMesh.Object;
staticMesh->SetStaticMesh(PlaneAsset);
staticMesh->SetRelativeRotation(FRotator(90, 0, 0));
staticMesh->SetRelativeScale3D(FVector(2, 2, 2));
staticMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);

SetRootComponent(collisionBox);
hitBox->SetupAttachment(collisionBox);
staticMesh->SetupAttachment(collisionBox);
}

I made a subclass of this pawn as a blueprint for changing some defaults on additional components I have, but it doesn’t change any of these components yet. When I open it in the editor, it renders as expected.

As soon as I go to spawn it into the level though, the child box component (hitBox) and static mesh component (staticMesh) spawn at the origin and don’t appear attached to the root component at all. Even weirder though is I have a basic jump function I’ve built into my starter movement component, and when I jump after starting the game, the components do move with the root box component. They’re just in the wrong place.

Am I doing something wrong? I mean clearly I am but I have no idea what that could be.

Also, once I’ve dropped the actor into the world, moving the actor around moves the whole thing, including the two components which spawned at the origin.

Aha! I found it.

Setting the relative location to 0 in OnConstruction fixed the problem. I don’t know if this is the right way to do it, but it worked.


void APAWN_2DCharacter::OnConstruction(const FTransform& Transform)
{
Super::OnConstruction(Transform);
staticMesh->SetRelativeLocation(FVector(0));
hitBox->SetRelativeLocation(FVector(0));
}

1 Like

I found setting this in the constructor easier.

sphereComponent->AttachToComponent(meshComponent, FAttachmentTransformRules::KeepRelativeTransform);