Hi i’m doing a pickup and drop in c++, but a i have a problem when i detach the object from the contoller, the object stays floating, I know it is because i need to set simulate physics on true but when I call the the function that changes physcs doesn’t work.
This is my cpp Pickupbable Object:
// Fill out your copyright notice in the Description page of Project Settings.
#include "PickupObject.h"
// Sets default values
APickupObject::APickupObject()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
//this->CharMovement->GetNavAgentPropertiesRef().bCanCrouch = true;
Sphere = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere"));
Sphere->SetupAttachment(GetRootComponent());
StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
StaticMesh->SetupAttachment(Sphere);
}
// Called when the game starts or when spawned
void APickupObject::BeginPlay()
{
Super::BeginPlay();
Sphere->SetSimulatePhysics(false);
Sphere->SetCollisionEnabled(ECollisionEnabled::PhysicsOnly);
}
// Called every frame
void APickupObject::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void APickupObject::EnablePhysics(bool Boolean) {
//Sphere->SetPhysicsLinearVelocity(GetActorLocation()*800);
Sphere->SetSimulatePhysics(Boolean);
GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Blue, FString::Printf(TEXT("Change Physics")));
}
And this is my my call from my controller.cpp when release the object:
bool AVRController::ReleasePickup() {
if (bIsPickingup) {
bIsPickingup = false;
APickupObject* PickupObject = Cast<APickupObject>(GetWorld()->SpawnActor(APickupObject::StaticClass()));
PickupObject->EnablePhysics(true);
TArray<AActor*> OverlappingActors;
GetOverlappingActors(OverlappingActors);
for (AActor* OverlappingActor : OverlappingActors) {
OverlappingActor->DetachAllSceneComponents(MotionController, FDetachmentTransformRules::KeepWorldTransform);
return 0;
}
}
return 1;
}