C++ and nested USceneComponents

So Unreal docs are giving me a hard time to do a basic thing again. I want to have relative locations on nested child USceneComponents to a parent USceneComponent which is then attached to the root component. The root component is a static mesh (in my case the weapon). So what I did was creating a USceneComponent called „UCollisionSystemComponent“ and that one has two child USceneComponents called SphereTraceStart and SphereTraceEnd. These are used (their location) for a sphere trace on the weapon blade. Well guess what happened of course. Instead of being relative to the weapon mesh it always did the trace at 0,0,0. So I cant find any solution in the docs or if nested scene components with relative location to the root component is even possible. Maybe you guys have a clue. At this point im just sick of the trail and error because of the lack of proper docs…

To give some more context, this is how the blueprint looks right now:

image

And here is the code for the weapon constructor:

CollisionSystemComponent = CreateDefaultSubobject<UCollisionSystemComponent>(TEXT("Collision System"));
	CollisionSystemComponent->SetupAttachment(GetRootComponent());

And this is then the constructor of the collision system component:

SphereTraceStart = CreateDefaultSubobject<USceneComponent>("TraceStart");
SphereTraceEnd = CreateDefaultSubobject<USceneComponent>("TraceEnd");
SphereTraceStart->AttachToComponent(this,FAttachmentTransformRules::KeepRelativeTransform);
SphereTraceEnd->AttachToComponent(this, FAttachmentTransformRules::KeepRelativeTransform);

But SphereTraceStart and SphereTraceEnd location is not relative to the root component but to the world origin. Hope you have an idea :slight_smile:

Hello,
Did you try using SnapToTarget instead of KeepRelativeTransform as attachment rule??

Just tried that, no success. But I found throughout trail and error one working solution which seems to be not the correct way but somehow it works as expected:

Weapon.cpp

AWeapon::AWeapon()
{
	PrimaryActorTick.bCanEverTick = true;
	CollisionSystemComponent = CreateDefaultSubobject<UCollisionSystemComponent>(TEXT("Collision System"));
	if(CollisionSystemComponent)
	{
		CollisionSystemComponent->SphereTraceStart->SetupAttachment(RootComponent);
		CollisionSystemComponent->SphereTraceEnd->SetupAttachment(RootComponent);
	}

CollisionSystemComponent.cpp

UCollisionSystemComponent::UCollisionSystemComponent()
{
	SphereTraceStart = CreateDefaultSubobject<USceneComponent>("TraceStart");
	SphereTraceEnd = CreateDefaultSubobject<USceneComponent>("TraceEnd");
}

So apparently when setting up the two USceneComponents TraceStart and TraceEnd from the parent Actor, it works. But if I try to attach those two inside of the Actor Component to the Root which is a static mesh, it wont do it. Very strange… Btw I changed also the CollisionSystemComponent from USceneComponent to UActorComponent.