[projectile weapon] projectile is heading in the wrong direction

Hi,

I’m having problem with projectile heading elsewhere that my target.

I’m looking at Unreal Documentation, ShooterGame example.
My weapon shoots projectiles but it seems they always go to the same place.

The projectiles should go to the end of green line but it seems they’re heading to the same direction no matter at what angle I shoot.

void ASFSProjectileWeapon::Fire()
{
	FVector ShootDir = GetAimFromCamera();
	FVector StartLocation = GetMuzzleLocation();

	DrawDebugPoint(GetWorld(), ShootDir, 5.0f, FColor::Black, false, 20.0f, 0);
	UE_LOG(LogTemp, Warning, TEXT("ASFSProjectileWeapon::Fire(), ShootDir = %s"), *ShootDir.ToString());
	UE_LOG(LogTemp, Warning, TEXT("ASFSProjectileWeapon::Fire(), MuzzleLocation = %s"), *StartLocation.ToString());

	const float ProjectileRange = weaponRange;
	const FVector StartTrace = GetAimStartLocation(ShootDir);
	const FVector EndTrace = StartTrace + ShootDir * ProjectileRange;

	UE_LOG(LogTemp, Warning, TEXT("ASFSProjectileWeapon::Fire(), StartTrace = %s"), *StartTrace.ToString());
	UE_LOG(LogTemp, Warning, TEXT("ASFSProjectileWeapon::Fire(), EndTrace = %s"), *EndTrace.ToString());

	FHitResult Impact = WeaponTrace(EndTrace, StartTrace);
	DrawDebugLine(GetWorld(), StartTrace, EndTrace, FColor::Green, false, 10.0f, 0, 1.0f);
	SpawnProjectile(StartLocation, EndTrace);
FHitResult ASFSProjectileWeapon::WeaponTrace(const FVector& TraceFrom, const FVector& TraceTo) const
{
	FCollisionQueryParams QueryParams;
	QueryParams.AddIgnoredActor(this);
	QueryParams.AddIgnoredActor(GetOwner());
	QueryParams.bTraceComplex = true;
	QueryParams.bReturnPhysicalMaterial = true;

	FHitResult Hit(ForceInit);
	GetWorld()->LineTraceSingleByChannel(Hit, TraceFrom, TraceTo, COLLISION_WEAPON, QueryParams);
	
	return Hit;
}

FVector ASFSProjectileWeapon::GetAimStartLocation(const FVector& AimDir) const
{
	APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
	FVector OutStartTrace = FVector::ZeroVector;
	AActor* WeaponOwner = GetOwner();

	UE_LOG(LogTemp, Warning, TEXT("ASFSProjectileWeapon::GetAimStartLocation WeaponOwner = %s"), *WeaponOwner->GetActorNameOrLabel());

	if (PlayerController)
	{
		FRotator PlayerCameraRotation;
		PlayerController->GetPlayerViewPoint(OutStartTrace, PlayerCameraRotation);

		UE_LOG(LogTemp, Warning, TEXT("ASFSProjectileWeapon::GetAimStartLocation if (PlayerController) OutStartTrace: %s"), *OutStartTrace.ToString());
		UE_LOG(LogTemp, Warning, TEXT("ASFSProjectileWeapon::GetAimStartLocation if (PlayerController) AimDir =  %s"),  *AimDir.ToString());
		UE_LOG(LogTemp, Warning, TEXT("ASFSProjectileWeapon::GetAimStartLocation if (PlayerController), GetActorLocation() = %s"), *WeaponOwner->GetActorLocation().ToString());

		OutStartTrace = OutStartTrace + AimDir * ((WeaponOwner->GetActorLocation() - OutStartTrace) | AimDir);
	}

	UE_LOG(LogTemp, Warning, TEXT("ASFSProjectileWeapon::GetAimStartLocation,  return OutStartTrace %s"), *OutStartTrace.ToString());
	return OutStartTrace;
}
void ASFSProjectileWeapon::SpawnProjectile(FVector Origin, FVector_NetQuantizeNormal ShootDir)
{
	FTransform SpawnTransform(ShootDir.Rotation(), Origin);
	UE_LOG(LogTemp, Warning, TEXT("ASFSProjectileWeapon::SpawnProjectile ShootDir.Rotation() = %s"), *ShootDir.ToString());
	ASFSProjectileBase* Projectile = Cast<ASFSProjectileBase>(UGameplayStatics::BeginDeferredActorSpawnFromClass(this, ProjectileConfig.ProjectileClass, SpawnTransform));
	
	if (Projectile)
	{
		Projectile->SetOwner(this);
		Projectile->InitVelocity(ShootDir);
		UGameplayStatics::FinishSpawningActor(Projectile, SpawnTransform);
		UE_LOG(LogTemp, Warning, TEXT("ASFSProjectileWeapon::SpawnProjectile SpawnTransform = %s"), *SpawnTransform.ToString());
	}
}

and some logs: (single shot)

LogTemp: Warning: ASFSCharacterBase::Shoot()
LogTemp: Warning: ASFSProjectileWeapon::GetAimFromCamera(), CameraLocation X=-8003.944 Y=13828.796 Z=231.080
LogTemp: Warning: ASFSProjectileWeapon::GetAimFromCamera(), CameraRotation P=3.642107 Y=-79.581543 R=0.000000
LogTemp: Warning: ASFSProjectileWeapon::GetAimFromCamera(), Aim X=0.180 Y=-0.982 Z=0.064
LogTemp: Warning: ASFSProjectileWeapon::Fire(), ShootDir = X=0.180 Y=-0.982 Z=0.064
LogTemp: Warning: ASFSProjectileWeapon::Fire(), MuzzleLocation = X=-8004.216 Y=13454.047 Z=221.731
LogTemp: Warning: ASFSProjectileWeapon::GetAimStartLocation WeaponOwner = BP_PlayerCharacter
LogTemp: Warning: ASFSProjectileWeapon::GetAimStartLocation if (PlayerController) OutStartTrace: X=-8003.944 Y=13828.796 Z=231.080
LogTemp: Warning: ASFSProjectileWeapon::GetAimStartLocation if (PlayerController) AimDir =  X=0.180 Y=-0.982 Z=0.064
LogTemp: Warning: ASFSProjectileWeapon::GetAimStartLocation if (PlayerController), GetActorLocation() = X=-8038.319 Y=13518.063 Z=190.137
LogTemp: Warning: ASFSProjectileWeapon::GetAimStartLocation,  return OutStartTrace X=-7950.491 Y=13538.079 Z=249.895
LogTemp: Warning: ASFSProjectileWeapon::Fire(), StartTrace = X=-7950.491 Y=13538.079 Z=249.895
LogTemp: Warning: ASFSProjectileWeapon::Fire(), EndTrace = X=-7770.020 Y=12556.552 Z=313.419
LogTemp: Warning: ASFSProjectileWeapon::SpawnProjectile ShootDir.Rotation() = X=-7770.020 Y=12556.552 Z=313.419
LogTemp: Warning: ASFSProjectileBase::InitVelocity, ShootDirection: X=-7770.020 Y=12556.552 Z=313.419
LogTemp: Warning: ASFSProjectileWeapon::SpawnProjectile SpawnTransform = -8004.216178,13454.047344,221.731401|1.215945,121.749307,0.000000|1.000000,1.000000,1.000000

The starting point seems to be ok, The projectiles start from the muzzle but the end point is completely different.

It works now, one little thing was missing:

TransformRotation = UKismetMathLibrary::FindLookAtRotation(GetMuzzleLocation(), End);