SphereSweep always returns false

Dear all, I am trying to detect a hit using a SphereTraceSingle inside my character’s Tick().

When I put other characters, it detects correctly the hit, but if I create a Blueprint for random objects in the scene it does not detect anything.

I have tried ECC_WorldDynamic, ECC_WorldStatic… I have tried to put a static mesh inside the BP, a dynamic one, a static as a child of the dynamic…

Maybe I am missing some BP settings here that prevent a collision?

I am attaching the settings of my BP actor, which I am changing trying to find the faulty setting, and my code…

Any help is greatly appreciated!

	FHitResult HitResult;
	FCollisionQueryParams CollisionParams;
	CollisionParams.AddIgnoredActor(this);
	CollisionParams.bTraceComplex = true;

	float SphereRadius = 50.0f;
	FVector StartLocation = GetActorLocation() + GetActorForwardVector() * 150.0f;
	FVector EndLocation = StartLocation + GetActorForwardVector() * 500.0f;

	ETraceTypeQuery TraceType = UEngineTypes::ConvertToTraceType(ECC_WorldDynamic);

	bool IsHit = UKismetSystemLibrary::SphereTraceSingle(
			GetWorld(),
			StartLocation,
			EndLocation,
			SphereRadius,
			TraceType,
			false,
			TArray<AActor*>(),
			EDrawDebugTrace::ForOneFrame,
			HitResult,
			true
		);

	// if we hit something and it's interactable
	if (IsHit)
	{
		GEngine->AddOnScreenDebugMessage(0, 5.0f, FColor::Black,
			FString::Printf(TEXT("can interact with %s"), *HitResult.GetActor()->GetActorNameOrLabel()));
		DrawDebugSphere(GetWorld(), HitResult.ImpactPoint, SphereRadius, 12, FColor::Magenta, false, 1.0f);

		InteractableActor = HitResult.GetActor()->GetClass()->ImplementsInterface(UTOWInteractable::StaticClass()) ? HitResult.GetActor() : nullptr;
	}
	else
	{
		//GEngine->AddOnScreenDebugMessage(0, 10.0f, FColor::Black,
		//	FString::Printf(TEXT("cannot interact with anything")));
		InteractableActor = nullptr;
	}

@jcxz100h

Here is a scene demonstrating it in action with some extra debug.
sweep.zip (58.1 KB)

Edit: added bp and cpp interface implementation and interface execution for c++ and bp

Did you add your interface to the objects that are supposed to be interacted with??

If you want to call the interface interact then you need to execute it!

A quick overlook of the function:


void ASweepActor::CollisionTest()
{


	FHitResult HitResult;
	FCollisionQueryParams CollisionParams;
	CollisionParams.AddIgnoredActor(this);
	CollisionParams.bTraceComplex = true;

	float SphereRadius = 50.0f;
	FVector StartLocation = GetActorLocation() + GetActorForwardVector() * 150.0f;
	FVector EndLocation = StartLocation + GetActorForwardVector() * 500.0f;

	ETraceTypeQuery TraceType = UEngineTypes::ConvertToTraceType(ECC_WorldDynamic);

	bool IsHit = UKismetSystemLibrary::SphereTraceSingle(
		GetWorld(),
		StartLocation,
		EndLocation,
		SphereRadius,
		TraceType,
		false,
		TArray<AActor*>(),
		EDrawDebugTrace::ForOneFrame,
		HitResult,
		true
	);

	// if we hit something and its interactable
	if (IsHit)
	{
		GEngine->AddOnScreenDebugMessage(0, 0.2f, FColor::Black,
			FString::Printf(TEXT("trace detects %s"), *HitResult.GetActor()->GetActorNameOrLabel()));
		DrawDebugSphere(GetWorld(), HitResult.ImpactPoint, SphereRadius, 12, FColor::Magenta, false, 1.0f);
		InteractableActor = HitResult.GetActor()->GetClass()->ImplementsInterface(UTOWInteractable::StaticClass()) ? HitResult.GetActor() : nullptr;

		if (InteractableActor != nullptr) {			
				// c++
				if (ITOWInteractable* inter = Cast<ITOWInteractable>(InteractableActor)) {
					inter->Interact_Implementation(InteractableActor);
				}
				// blueprint (need overides implemented from within bp)

				if (InteractableActor->GetClass()->ImplementsInterface(UTOWInteractable::StaticClass()))
				{
					ITOWInteractable::Execute_Interact(InteractableActor, this);
				}			
		}

		GEngine->AddOnScreenDebugMessage(1, 0.2f, FColor::Green,
			FString::Printf(TEXT("current interactable %s"), *InteractableActor->GetActorNameOrLabel()));
	}
	else
	{
		InteractableActor = nullptr;
	}
}

Thanks for the reply @3dRaven !

I have read your code and, well, I am not good enough to see my problem! :frowning: I’m a noob in this…

I have uploaded part of my project on DropBox, since I am not allowed uploads yet…

I have purged few things in my project to make it smaller in the Contents folder:

  • The Characters containing Mannequin_UE4 and Mannequins
  • The StarterContent that I use to have something to play with

What I have noticed moving the door that shoul open upon interaction, is that it opens automatically! That is weird…

If you want to try my code you can set three players and act as a listen server, then you’ll see that character-character I have hits and possible interaction, but not with the door…

Thanks for your help!

My code called for interaction on tick for demonstrative purposed only. It should be called just once per interaction. I’ll check out your code in a moment.

Ok so I modified you code only slightly.

Added the interaction once the interact actor is detected. The main problem was that your door mesh lacked collision in the static mesh (you have to open up the sm_door asset and add the collision there, it was empty).

Also your timeline had autoplay on that triggered the door animation on start.

1 Like

Thanks @3dRaven !

I would have never thought about looking at the mesh if it has a collider! You saved another day of aimless wandering!

Might I ask one thing? The mesh has a setting “Generate missing collision”, it does apparently nothing? I am puzzled…

…but happy because you made it work!

Thank you! :slight_smile:

“Generate missing collision” might just work on import. If the collision was removed at some point in the project’s lifecycle after it’s in the content folder, then it will stay without it until it’s manually added.

1 Like

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