How can I detect a nearby actor to trigger a method? C++

Hi guys!
I’m currently trying to trigger a camera shake on the Sentinal character when the Shrouded character comes nearby. Currently the ‘DetectNearbyShrouded’ method is called every tick which gets the Sentinal location and Shrouded location then compares them using FVector::Distance before using a if statement to say if they’re within a range of 200 start the camera shake. I think maybe I am casting wrong when getting the Shrouded’s location? I’ve already tried using a Box collision attached to the Sentinal combined with OnBeginOverlap and didn’t have much luck.

Any help would be appreciated :slight_smile:

Sentinal.h

UFUNCTION()
		void DetectNearbyShrouded(AActor* OtherActor);  

Sentinal.cpp

void ASentinal::DetectNearbyShrouded(AActor* OtherActor)
{
	FVector SentinalLoc;
	SentinalLoc = GetActorLocation();

	AShrouded* ShroudedReference = Cast<AShrouded>(OtherActor);
	FVector ShroudedLoc = ShroudedReference->GetActorLocation();

	float Distance = FVector::Distance(SentinalLoc, ShroudedLoc);

	if (Distance > 200.0f)
	{
		GetWorld()->GetFirstPlayerController()->PlayerCameraManager->StartMatineeCameraShake(ShakeCamera, 6.0f);
		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, TEXT("Shake"));
	}
	else {
		GetWorld()->GetFirstPlayerController()->PlayerCameraManager->StopAllCameraShakes();
		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, TEXT("Stop Shake"));
	}
}

Hello! Is this correct condition? Or there should be less operator?

Distance > 200.0f

Hi Kehel, yes you’re right. I’ve since swapped the operator but after some troubleshooting I’ve found that the cast is failing.