AI won't shoot at player

So I have set up the AI’s behavior tree that supposes to let the AI attacks player when in sight.

But for some reason, the AI would not focus and shoot at the player. Targeting player was made using BP.

I can’t tell why it isn’t working.

Hi, you could watch your behavior tree as the game runs, and then look at what branches it executes and what the blackboard values are. Then see where it does something you would not expect and step by step continue from there / post images of that code and behavior (cause from your shown code and text I don’t understand what you want it to do, nor what goes wrong).

Hi, sorry for not making myself clear.
What I am trying to do is having the AI focus on the player character once the AI detects it, then it will try to find a cover and then shoot at the player character. So far the AI does focus on the player as the spring arm and the camera were rotated towards the player:

And I have double checked the AI’s behavior, and it did set my player character as the TargetActor (Black board value), so it does recognize the actor it needs to find, focus, and fire at:

After gave it a few try, I noticed that only the AI’s spring arm plus the camera were focused and rotated towards the player character, but not the AI’s character’s mesh. And I think this is where the problem is. In my AI weapon class, when the gun is being fired, the mesh of the character that is holding the gun will fire a line trace (red debug lines in the pics), and deals damage to the character it hits. And so far, the AI’s mesh is not rotating with the spring arm and the camera. Thus, when the gun is being fired, even the AI was able to focus on the player, the mesh was not pointing towards somewhere else, therefore the line trace will not hit the player’s character:

I am having a hard time to identify what is causing this issue. I am guessing it might because of how I defined the character’s rotation and movement, but not completely sure:

In APlayerCharacter::Rotating you set the yaw angle as the pitch rotation (and I’m not sure whether you actually can rotate the capsule on pitch), not as the yaw rotation. It is (roll, pitch, yaw) in the rotation.

Can you show an image of the task ShootAtPlayer?


Generally speaking, for rotating the AI you have a RotateToFaceBB entry task that you could use. And you can also use a blendspace in the animation on top to make the upper body of the AI face the target for pitch rotation.

Thanks for the reply!
Here is the task for shoot at player:

It calls the AIShoot function.
And the funny thing is, I just made a behavior tree for AI to do melee attack, and this time the AI can focus on my player:

So the Set Default Focus to Player actually works in this behavior tree, and the AI is dealing damage as expected as well. It seems that it is not working properly only in the shooting behavior tree.
I also tried the RotateToFaceBB and whenever the AI sighted the player, it would just stuck in the RotateToFaceBB entry until I move my character in front of the AI’s character’s face, then the AI will shoot and proceeds to other actions (not at me, but the first location where I appeared in front of AI’s face)

As of the Yaw angle as pitch rotation, I am making a top-down game and the character can only rotate left and right.

Ok, could you show the code from AIShoot up to where you rotate the AI? Cause I don’t see that it can rotate via APlayerCharacter::Rotating, because there you set the yaw rotation (the left right one) to just zero.

Of course!

//AI Gun Spawn
	FActorSpawnParameters GunParams;
	GunParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
	StarterGun = GetWorld()->SpawnActor<AAIWeapon>(StarterWeapon, FVector::ZeroVector, FRotator::ZeroRotator, GunParams);
	if (StarterGun)
	{
		StarterGun->SetOwner(this);
		StarterGun->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetIncludingScale, FName("HoldWeapon"));
	}

I made a weapon class for AI and used this to spawn a gun for the AI through Blueprint.

As for calling the fire function, I used this:

void APlayerCharacter::AIShoot()
{
	StarterGun->Fire();
}

And here is the gun implementation the AI is using:

AAIWeapon::AAIWeapon()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	GunMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("GunMesh"));
	SetRootComponent(GunMesh);

}

void AAIWeapon::Fire()
{
	AActor* MyOwner = GetOwner();
	FHitResult Hit;
	if (MyOwner)
	{
		FVector EyeLocation = MyOwner->GetActorLocation();
		FRotator EyeRotation = MyOwner->GetActorRotation();
		FVector FireDirection = EyeRotation.Vector();
		FVector End = EyeLocation + (EyeRotation.Vector() * 10000); 
		FCollisionQueryParams HitParams;
		HitParams.AddIgnoredActor(MyOwner);
		HitParams.AddIgnoredActor(this);
		
		if (GetWorld()->LineTraceSingleByChannel(Hit, EyeLocation, End, ECollisionChannel::ECC_GameTraceChannel1, HitParams))
		{
			AActor* HitActor = Hit.GetActor();
			UGameplayStatics::ApplyPointDamage(HitActor, 10.f, FireDirection, Hit, MyOwner->GetInstigatorController(), this, DamageClass);
		}
		DrawDebugLine(GetWorld(), EyeLocation, End, FColor::Red, false, 30.f);
		UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactFlash, Hit.Location, FireDirection.Rotation());
	}

	UGameplayStatics::SpawnEmitterAttached(MuzzleFlash, GunMesh, TEXT("MuzzleFlash"));
}

As for the rotation, I made my AI Blueprint based on the PlayerCharacter class, so the movement implementation is the same as the player’s. I used the default AIController for AI’s controller class, so I assumed the actual AI movement would still be different from the player’s since that should override my codes. I did not make an AI controller class.
These are the movement and rotation codes:

void APlayerCharacter::MoveForward(float AxisValue)
{

	FVector DeltaLocation (0.f);
	DeltaLocation.X = AxisValue * 10.f * UGameplayStatics::GetWorldDeltaSeconds(this);
	GetCharacterMovement()->MaxWalkSpeed = 400.f;
	AddMovementInput(FVector(1.0, 0.0, 0.0) * AxisValue);
}

void APlayerCharacter::Moveright(float AxisValue)
{

	FVector DeltaLocation (0.f);
	DeltaLocation.Y = AxisValue * 10.f * UGameplayStatics::GetWorldDeltaSeconds(this);
	GetCharacterMovement()->MaxWalkSpeed = 400.f;
	AddMovementInput(FVector(0.0, 1.0, 0.0) * AxisValue);

}


void APlayerCharacter::Rotating(FVector Look)
{
	FVector ToTarget = Look - APlayerCharacter::GetMesh()->GetComponentLocation();
	FRotator LookRotation = FRotator(0.f, ToTarget.Rotation().Yaw, 0.f);
	APlayerCharacter::GetCapsuleComponent()->SetWorldRotation(LookRotation);
}

And thank you for your patience! It is my first game project, everything is soooo messy that even myself got lost in my own codes and stuff many times

Ok, so you’re not rotating it in there (and I also don’t see anything in there that would make RotateToFaceBBEntry not rotate correctly). Could you show an image of LookAtPlayer?


That below used with a default character rotates to always face CurrentTarget actor.

And in the AI controller just

1 Like

You were right!! The AI was not rotating correctly as it was using the player’s Rotating function (rotate to cursor), and I also made a simple “add controller yaw input” just for the AI, but by default it was always trying to use my rotating-to-cursor function, so it did not rotate correctly. So I check the “Use Controller Rotation Yaw” in the AI blueprint’s default setting and just let it use the AIController:
image
Then everything worked!!
Thank you for letting me know the AI was not rotating correctly! I was solely focusing on Camera since I thought that was giving me all these problems.