I’m struggling with the AI Shooting to it’s left. I (The player) shoots correctly.
Here’s the code. I’m not sure if there were any changes to the AI’s viewpoint between UE4 and UE5.2.
void AGun::PullTrigger()
{
UGameplayStatics::SpawnEmitterAttached(MuzzleFlash, MeshComponent, TEXT("MuzzleFlashSocket"));
//Declaration of Variables and QueryParameters for the gun's bullet
FVector PlayerViewPointLocation;
FRotator PlayerViewPointRotation;
FHitResult HitResult;
FCollisionQueryParams QueryParams;
QueryParams.AddIgnoredActor(this);
QueryParams.AddIgnoredActor(GetOwner());
//Getting Viewpoint and TargetLocation(TraceEndLocation) for linetrace
APawn* OwnerPawn = Cast<APawn>(GetOwner());
if (OwnerPawn == nullptr) return;
AController* OwnerController = OwnerPawn->GetController();
if (OwnerController == nullptr) return;
OwnerController->GetPlayerViewPoint(PlayerViewPointLocation, PlayerViewPointRotation);
FVector TraceEndLocation = PlayerViewPointLocation + PlayerViewPointRotation.Vector() * 10000;
//Conduct Line Trace
bool bHitSuccess = GetWorld()->LineTraceSingleByChannel(HitResult, PlayerViewPointLocation, TraceEndLocation, ECollisionChannel::ECC_GameTraceChannel1, QueryParams);
if (bHitSuccess)
{
if (ImpactEffect != nullptr)
{
//Spawn MuzzleFlash
FVector ShotDirection = -PlayerViewPointRotation.Vector();
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactEffect, HitResult.Location, ShotDirection.Rotation());
//Send Damage if the target hit is an Actor
if (HitResult.GetActor() != nullptr)
{
FPointDamageEvent DamageEvent(Damage, HitResult, ShotDirection, nullptr);
HitResult.GetActor()->TakeDamage(Damage, DamageEvent, OwnerController, this);
}
}
}
}