Hello, I will put some screenshot of my issue :
My Line trace works 1 time out of 50…
This is my code :
void UGrabber::BeginPlay()
{
Super::BeginPlay();
FindPhysicsHandleComponent();
SetupInputComponent();
}
void UGrabber::GrabObject(){
UE_LOG(LogTemp, Warning, TEXT("GRAB PRESSED"));
GetFirstPhysicsBodyInReach();
}
void UGrabber::ReleaseObject()
{
UE_LOG(LogTemp, Warning, TEXT("GRAB RELEASED"));
}
// ERROR LOG IF !PhysicsHandle componant
void UGrabber::FindPhysicsHandleComponent()
{
/// Look fr attached Physics Handle
PhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
if (!PhysicsHandle) {
UE_LOG(LogTemp, Error, TEXT("%s doesn't found physics handle component."), *(PhysicsHandle->GetName()));
}
}
// Bind input with functions
void UGrabber::SetupInputComponent()
{
InputComponent = GetOwner()->FindComponentByClass<UInputComponent>();
if (InputComponent) {
InputComponent->BindAction("Grab", IE_Pressed, this, &UGrabber::GrabObject);
InputComponent->BindAction("Grab", IE_Released, this, &UGrabber::ReleaseObject);
}
else {
UE_LOG(LogTemp, Error, TEXT("%s doesn't found input component."), *(InputComponent->GetName()));
}
}
// Display LOG if grab object
FHitResult UGrabber::GetFirstPhysicsBodyInReach() const
{
FVector PlayerViewPointLocation;
FRotator PlayerViewPointRotation;
FHitResult Hit;
FVector LineTraceEnd = PlayerViewPointLocation + PlayerViewPointRotation.Vector() * Reach;
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(
OUT PlayerViewPointLocation,
OUT PlayerViewPointRotation
);
// Setup query parameters
FCollisionQueryParams TraceParameters(FName(TEXT("")), false, GetOwner());
// Draw a line-trace in the world to reach distance
GetWorld()->LineTraceSingleByObjectType(
OUT Hit,
PlayerViewPointLocation,
LineTraceEnd,
FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
TraceParameters);
AActor* ActorHit = Hit.GetActor();
UE_LOG(LogTemp, Warning, TEXT("Actor is %s"), (*ActorHit->GetFullName()));
if (ActorHit) {
UE_LOG(LogTemp, Warning, TEXT("HitActorName is %s."), *(ActorHit->GetName()));
}
return FHitResult();
}
Thank you guys (Sorry for my english )