Greetings!
I’m trying to program the boomerang throwing mechanic. So the boomerang I have attached to the hand with a child object component. When I launch it, I undo it from the hand with a DetachFromComponent and activate the physics with a SetSimulatePhysics of the boomerang. Then when the boomerang is going to regrease I deactivate the physics and when it is close to the player I match it to the hand with AttachToComponent, but the latter does not work. . I have seen on the internet that it gives problems to use AttachToComponent and SetSimulatePhysics and that it should be the object that activates and deactivates the physical rootcomponent but in this case the boomerang should be related to the hand.
Adjunto las lines de codigo donde hago esas acciones.
Constructor of my Player class.
boomerangChild = CreateDefaultSubobject<UChildActorComponent>(TEXT("BoomerangChild"));
boomerangChild->SetupAttachment(Mesh1P,"hand_rSocket");
When the player throws the boomerang.
void ABoomerangUE4Character::LaunchBoomerang()
{
boomerangChildLocation = boomerangChild->GetRelativeLocation();
boomerangChildRotator = boomerangChild->GetRelativeRotation();
boomerangChild->DetachFromComponent(FDetachmentTransformRules::KeepWorldTransform);
boomerangActor->LaunchBoomerang();
FTimerHandle timerHandle;
GetWorld()->GetTimerManager().SetTimer(timerHandle,this,&ABoomerangUE4Character::TakeBoomerang,3,false);
}
Launch of the Actor Boomerang
void ABoomerang::LaunchBoomerang()
{
mesh->SetSimulatePhysics(true);
mesh->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Block);
mesh->AddImpulse(GetActorForwardVector()*speed);
launchBoomerang = true;
}
When it returns in the player
void ABoomerangUE4Character::TakeBoomerang()
{
boomerangActor->ReturnBoomerang();
boomerangChild->AttachToComponent(Mesh1P,FAttachmentTransformRules::KeepWorldTransform,"hand_rSocket");
boomerangChild->SetRelativeLocation(boomerangChildLocation);
boomerangChild->SetRelativeRotation(boomerangChildRotator);
}
When he returns in the boomerang actor
void ABoomerang::ReturnBoomerang()
{
mesh->SetSimulatePhysics(false);
mesh->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Overlap);
returnBoomerang = true;
}
The Tick function of the Boomerang Actor
if(returnBoomerang)
{
FVector playerLocation = GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorLocation();
SetActorLocation(FMath::Lerp(GetActorLocation(),playerLocation,timeToReturn/timeToRegres));
timeToReturn += DeltaTime;
if(FVector::Distance(playerLocation,GetActorLocation()) <= 50)
{
timeToReturn = 0;
launchBoomerang = false;
returnBoomerang = false;
}
}
I don’t know how to fix this.
Thanks a lot.