Actors aren't showing up in sweep

For some reason, actors aren’t being hit but the code works if it were set in the Tick and called every frame so I’m not sure what’s wrong.

void UGrabber::Grab(const FInputActionValue& Value)
void UGrabber::Grab(const FInputActionValue& Value)
{
	const bool GrabValue = Value.Get<bool>();


	
	if(GrabValue)
	{
		

		FVector Start = GetComponentLocation();
		FVector End = Start + GetForwardVector() * MaxGrabDistance;
		DrawDebugLine(GetWorld(), Start, End, FColor::Emerald);

		FCollisionShape Sphere= FCollisionShape::MakeSphere(GrabRadius);
		FHitResult HitResult;
		bool HasHit = GetWorld()->SweepSingleByChannel(
			HitResult, 
			Start, End, 
			FQuat::Identity, 
			ECC_GameTraceChannel2,
			Sphere
			);

		UE_LOG(LogTemp, Display, TEXT("If you see this it works!"));
		UE_LOG(LogTemp, Display, TEXT("Start: %s"), *Start.ToString());
		UE_LOG(LogTemp, Display, TEXT("ForwardVector is: %s!"), *End.ToString());


		if (HasHit)
		{
			AActor* HitActor = HitResult.GetActor();
			UE_LOG(LogTemp, Display, TEXT("Hit actor: %s"), *HitActor->GetActorNameOrLabel());
			
			
		}else{
			UE_LOG(LogTemp, Display, TEXT("You don't see anything"));
			UE_LOG(LogTemp, Display, TEXT("Start: %s, End: %s, SphereRadius: %f"), *Start.ToString(), *End.ToString(), Sphere.GetSphereRadius());
    		
		}

	
	}
}

Hi,

Do you mean that the code records hits when called under Tick, but not if you call it with a buton press? And I assume th function name duplication at the top is a typo…

I assume that your GrabValue is True (say, when you hold ‘G’ to grab) so, is HasHit returning false? Is the DrawDebugLine working (can you see the line if you F8 out of the pawn)?

A debug sphere will be more useful to work out what is going on:

DrawDebugSphere(GetWorld(), HitResult.ImpactPoint, 10, 16, FColor::Red, false, 5);

It should appear on the actor being hit.

I can’t see anything worng in the code, I suspect it is in the action mapping if it works in tick but not through the input call. Are you coding CryptRaider by any chance? If you are using UE5 you will need to modify the FirstPerson Enhanced Input mapping instead of the legacy ‘normal’ input mapping, by adding a Grab action to IMC_Default and copy IA_Look to IA_Grab (change the Value Type to Axis1D(float)).

Thank you

Yes, the duplication at the top is a typo.

The GrabValue is true and HasHit is returning false.
The DrawDebugLine is NOT working unless I put it under the Tick and when I do it’s almost completely horizontal.

When I DrawDebugSphere, for some reason there is a DrawDebugLine and Sphere in the map. (Made the floor black to see the line and the character is the green line to the left)

Yes, I am coding CryptRaider.
I went through the recommended videos and I believe the action mappings are correct though my IA_Grab is a bool.

I had a similar issue with the grabber being horizontal. From memory the problem was the component it is attached to. It needs to be attached to the camera, not the root or other component as the camera’s forward vector is the only bit that isn’t flat… This is mine:

playerComponents

Also, unless they updated it the vidoe does not voer the Enhanced Input used by UE5. That caused problems for me until I worked out how to use the new system. My IA_Grab is based on the following [actually, I can only add one picture, so I’ll reply separately to add the input images]:

TBA

I can’t tell why the DrawDebugLine isn’t working except when in Tick. I can only guess that the input isn’t setting GrabValue to true. Is your screenshot from when the code is run on Tick or on input? If it is from input then clearly GrabValue is true to get the UE_LOGs.

Perhaps the component attachment will be the fix, as the horizontal trace will never hit anything if you’re not facing a wall, so try that first. I can send you my project to explore if this doesn’t fix it, and you can track down when mine differs.

IA_Grab file
IA_Grab

IMC_Default file and player EventGraph

I got it working
Turns out the GetComponetLocation() or ‘Start’ was incorrect which was why I was seeing through the floor.
I got the line on the character now but I need to somehow attach it to the camera so it matches the POV.
No idea why it worked in Tick

Also, I would love for you to share your project so I can have a reference with EnhancedInput settings.

Many Thanks

Good work.
You can find the key parts of mine here: GitHub - Mattat01/CryptRaider
It will be missing most of the assets so the level may look strange or not render but the input (Content\FirstPerson\Input modified from the default UE First Person template) and player blueprint (BP_Player) should be the only parts you need.

Good Luck