LineOfSightTo

I have a question about Line Of Sight To.
I know that if I move near AI, AI will stare at me. By the way, when I made a project by myself and ran it today, AI attacks me, but AI doesn’t look back on me even though I’m behind it.
In a nutshell, if the player moves when there is a player near the AI, the AI does not look at the player.

Isn’t Line Of Sight To supposed to be viewed in the direction of the player?

if I recall, there’s a player perception component that lets you set the radius of detection. This would be used with Behaviour trees and lets you detect the player in the relevant tasks.

Thank you for answering me first.
Are you talking about AIPerception? If it is AiPerception. Do I have to use AIPerception together to make the NPC look at me?

I created a task to make the AI look at me as it attacked. I wrote the code as below, but the AI never spins. It doesn’t intend to look at me…

EBTNodeResult::Type UBTTask_Turn::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
	Super::ExecuteTask(OwnerComp, NodeMemory);

	APawn* NPC = OwnerComp.GetAIOwner()->GetPawn();
	if (NPC == nullptr)
	{
		return EBTNodeResult::Failed;
	}

	FName PlayerName = TEXT("Target");
	APawn* Player = Cast<APawn>(OwnerComp.GetBlackboardComponent()->GetValueAsObject(PlayerName));	
	if (Player == nullptr)
	{
		return EBTNodeResult::Failed;
	}

	float TurnSpeed = 1.0f;
	FVector LookTargetVector = Player->GetActorLocation() - NPC->GetActorLocation();
	FRotator LookTarget = FRotator(0.0f, LookTargetVector.Rotation().Yaw, 0.0f);	
	NPC->SetActorRotation(FMath::RInterpTo(NPC->GetActorRotation(), LookTarget, GetWorld()->GetDeltaSeconds(), TurnSpeed));	

	return EBTNodeResult::Succeeded;
}

You’re successfully completing the task so it won’t complete the rotate.

So how do you rotate it? I tried it as a service, but it didn’t work. Below is the service code.

void UBTService_SeePlayer::TickNode(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds)
{
	Super::TickNode(OwnerComp, NodeMemory, DeltaSeconds);

	APawn* Player = UGameplayStatics::GetPlayerPawn(GetWorld(), 0);	
	AAIController* AIController = OwnerComp.GetAIOwner();	
	APawn* NPC = AIController->GetPawn();	


	if (AIController->LineOfSightTo(Player))
	{
		UWorld* World = NPC->GetWorld();

		FName PlayerPositionName = TEXT("PlayerPos");
		FVector PlayerPosition = Player->GetActorLocation();	
		OwnerComp.GetBlackboardComponent()->SetValueAsVector(PlayerPositionName, Player->GetActorLocation());
		
		FName TargetName = TEXT("Target");
		OwnerComp.GetBlackboardComponent()->SetValueAsObject(TargetName, Player);	
		

		DrawDebugPoint(World, Player->GetActorLocation(), 10.0f, FColor::Green, false, 0.2f);
		DrawDebugLine(World, NPC->GetActorLocation(), Player->GetActorLocation(), FColor::Green, false, 0.27f);
	}
	else
	{
		FName TargetName = TEXT("Target");
		OwnerComp.GetBlackboardComponent()->SetValueAsObject(TargetName, nullptr);	
	}
}

Succeeded for a task, in this case anyway, should be returned only when the enemy rotates within a margin of error to the player. If you want them to rotate and move at the same time, that’s more complicated.

So what should I do…? When I took the lecture, when the AI moved near me, it kept looking at me and following me. But what I’m making this time is a game of fighting with fists, but if it’s within the attack radius, it doesn’t turn even if the player is behind me.

I honestly am not sure. I’m too busy at the moment to help with out of course stuff but I’d ask in the discord. it’s the best place to ask these things.

1 Like

Privacy & Terms