I want to attach an actor to another actor when they collide.
I wrote the following code but although it shows the debug message, AttachToComponent
always return false.
The OtherActor
is the player control mannequin, and the ACubeActor
is a cube with static mesh, which I created by following one of the tutorial that I found on UE5’s official documentation.
void ACubeActor::OnHit(UPrimitiveComponent *HitComp, AActor *OtherActor, UPrimitiveComponent *OtherComp, FVector NormalImpulse, const FHitResult &Hit)
{
if (OtherActor != NULL)
{
GEngine->AddOnScreenDebugMessage(-1, 1., FColor::Red, OtherActor->GetName());
auto got = AttachToComponent(
OtherActor->GetRootComponent(),
FAttachmentTransformRules::SnapToTargetIncludingScale
);
GEngine->AddOnScreenDebugMessage(-1, 1., FColor::Red, got ? "true" : "false");
}
}
I’m new to Unreal and any help is appreciated.