UE5.2 AI Shoots to the left (Lecture 193 Udemy UE5 C++ Developer Course)

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);
			}
		}
	}

}

Hello @Wingpoo Can You Make Zip [ Content, Config, Source, .uproject ] And Send Me?

I had changed the actor viewpoint to the gun and accidentally included the guns rotation (which was 90 degrees to the left). Removing the change of rotation fixed my issue, it took sleeping to notice that.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms