AI Detect if player is in collision box

I am a bit confused on this part of the learning curve if I want my AI to do something only if the player is in the collision box and not just another Actor. I have the current function:

void ANPCHouse1::OnOverlapBegin(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	if (OtherActor && (OtherActor != this) && OtherComp)
	{
		CapsuleComponent->SetVisibility(true);
		GetMesh()->SetVisibility(true);
	}
}

void ANPCHouse1::OnOverlapEnd(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	if (OtherActor && (OtherActor != this) && OtherComp)
	{
		CapsuleComponent->SetVisibility(false);
		GetMesh()->SetVisibility(false);
	}
}

However these get triggered if the NPC detect another NPC or actor for that matter. How can I get an instance of the current player’s actor so it only runs if a player is in the collision box?

You’ll want to add the following to your code.

void ANPCHouse1::OnOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
	if (OtherActor == PlayerCharacter)
	{


	}
}

void ANPCHouse1::Tick()
{
   	for (TActorIterator<(YourCharacterClass)> Arz(GetWorld()); Arz; ++Arz) //Replace the parenthesis with your character class.
	{
		PlayerCharacter = *Arz;
	}
}

//

// In your .H file add the following. Remove the parenthesis of course.

(YourCharacterClass) * PlayerCharacter;

What is Arz?

Arz is the name of the iterator variable created in the for loop. You can change it to whatever you want. :slight_smile: