GrabSelector works rarely

Hello, I will put some screenshot of my issue :

Bug%20grab2

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 :slight_smile: (Sorry for my english :smile: )

What is your Reach value?

You’re returning a default constructed FHitResult here instead of HitResult

By reach value I understand “Hit” value :
UE_LOG(LogTemp, Warning, TEXT("Hit is %s"), Hit.GetActor());

In fact, my linetrace works, but only in very special angles, it’s really weird

I found why !!!

Just because I used
FVector LineTraceEnd = PlayerViewPointLocation + PlayerViewPointRotation.Vector() * Reach;
before it was initialised by

GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(
		OUT	PlayerViewPointLocation,
		OUT	PlayerViewPointRotation
	);

I inverted this code and now it’s works !

Thank you for take time for me ! :slight_smile:

Ah! That would do it.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms