Hi,
I want to have the same functionality as the [Pick Up Physics Tutorial][1] but written in C++. I have already read the following questions in the hub without any success:
It compiles and it doesn’t crash. I have debugged it and it does seem to grab the Actor.
It also seems that PhysicsHandle belong to my character:
And when I debug the code, it seems to be fine, finishing the method correctly and assigning the grabbed actor.
When I debug the location of the “PhysicsHandle->CurrentTransform”…
GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Green, "PhysicsHandle Transform " + PhysicsHandle->CurrentTransform.ToString());
… the transform of the Physics Handle never moves.
I have the same sort of settings that the [Blueprint tutorial][6] has, even though I’ve tried to grab many different Actors:
This is my source code:
AMainCharacter.h
...
public:
UPROPERTY(VisibleDefaultsOnly, Category = PhysicHandle)
class UPhysicsHandleComponent* PhysicsHandle;
FHitResult Hit;
...
//MainCharacter.cpp
AMainCharacter::AMainCharacter() : Super()
{
GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
PhysicsHandle = CreateDefaultSubobject<UPhysicsHandleComponent>(TEXT("PhysicsHandle"));
PhysicsHandle->LinearDamping = 200.f;
PhysicsHandle->LinearStiffness = 750.f;
PhysicsHandle->AngularDamping = 500.f;
PhysicsHandle->AngularStiffness = 1500.f;
PhysicsHandle->InterpolationSpeed = 50.f;
PhysicsHandle->bRotationConstrained = true;
PhysicsHandle->SetActive(true);
PhysicsHandle->Activate();
}
void AMainCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
...
InputComponent->BindAction("Action", IE_Released, this, &AMainCharacter::TestGrab);
...
}
void AMainCharacter::GetUsableInView()
{
FVector camLoc;
FRotator camRot;
if (Controller) {
Controller->GetPlayerViewPoint(camLoc, camRot);
const FVector start_trace = camLoc;
const FVector direction = camRot.Vector();
const FVector end_trace = start_trace + (direction * MAX_USABLE_DISTANCE);
FCollisionQueryParams TraceParams(FName(TEXT("")), true, this);
TraceParams.bTraceAsyncScene = true;
TraceParams.bReturnPhysicalMaterial = false;
TraceParams.bTraceComplex = true;
GetWorld()->LineTraceSingleByChannel(Hit, start_trace, end_trace, COLLISION_PROJECTILE, TraceParams);
}
}
void AMainCharacter::TestGrab()
{
GetUsableInView();
const FVector grabLocation = GetActorLocation() + (GetActorRotation().Vector() * 150);
UPrimitiveComponent* PComponent = Hit.GetComponent();
PComponent->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Ignore);
if (!Hit.GetComponent()->HasBeenInitialized())
{
Hit.GetComponent()->InitializeComponent();
}
PhysicsHandle->GrabComponent(Hit.GetComponent(), Hit.BoneName, Hit.Location, false);
// I also tried this
// PhysicsHandle->GrabComponent(Hit.GetComponent(), FName("RightHandSocket"), grabLocation, false);
}
Please, help!!!
I have pretty much everything else done with C++, so I would prefer to keep developing in C++.
Many Thanks,
Bullyproof