Hi , this is my first post here. I’ve spent already a day, trying to solve this mystery.
Please let me know if my understanding of this is wrong. And why is my PlayerTank is not logging Aiming…
We have TankAIController that calculates where to aim (in this case towards PlayerTank location) and passes Hit Location to a Tank’s AimAt(), like this:
if (GetControlledTank())
{
GetControlledTank()->AimAt(GetPlayerTank()->GetTargetLocation());
}
//TODO Fire if ready
}
ATank* ATankAIController::GetControlledTank() const
{
return Cast<ATank>(GetPawn());
}
ATank * ATankAIController::GetPlayerTank() const
{
auto PlayerPawn = GetWorld()->GetFirstPlayerController()->GetPawn();
if (!PlayerPawn) { return nullptr; }
return Cast<ATank>(PlayerPawn);
}
We also have TankPlayerController, that calculates aiming towards crosshair by deprojecting screen position to world, and passes its Crosshair Hit Location to Tank AimAt(); as well. Like so:
void ATankPlayerController::AimTowardsCrosshair()
{
if (!GetControlledTank()) { return;} //if no tank then just exit
FVector PlayerTankHitLocation; //Out parameter
if (GetSightRayHitLocation(PlayerTankHitLocation)) //Has "side-effect", is going to ray trace
{
//UE_LOG(LogTemp, Warning, TEXT("PLAYER TANK CROSSHAIR AIMING AT: %s"), *(PlayerTankHitLocation.ToString()));
GetControlledTank()->AimAt(PlayerTankHitLocation);
}
}
NOTE: Here commented UE_LOG is successfully logging aiming coordinates.
This means that it also successfully passes HitLocation to Tank AimAt(); (For the clarity, I’ve renamed HitLocation to PlayerTankHitLocation).
Then Tank 's AimAt adds LaunchSpeed variable and passes HitLocation from both, TankAIController and TankPlayerController to TankAimingComponent’s AimAt(); Like so:
void ATank::AimAt(FVector HitLocation)
{
TankAimingComponent->AimAt(HitLocation, LaunchSpeed);
}
Then TankAimingComponent calculates where to aim for each tank, like so:
void UTankAimingComponent::AimAt(FVector HitLocation, float LaunchSpeed)
{
if (!Barrel) { return; }
FVector OUT LaunchVelocity;
FVector StartLocation = Barrel->GetSocketLocation(FName("Projectile"));
//Calculate the OUT LaunchVelocity
if (UGameplayStatics::SuggestProjectileVelocity(
this,
OUT LaunchVelocity,
StartLocation,
HitLocation,
LaunchSpeed,
false,
0,
0,
ESuggestProjVelocityTraceOption::DoNotTrace
)
)
{
auto AimDirection = LaunchVelocity.GetSafeNormal();
auto TankName = GetOwner()->GetName();
UE_LOG(LogTemp, Warning, TEXT("%s Aiming at %s"), *TankName, *AimDirection.ToString());
}
}
As you can see on the screenshot above, the problem is that all AI Tanks aiming direction is being logged except Tank_BP_C_0, which is a player tank. And there is no logging per tick for player tank at all, e.g. when I move mouse around, all numbers are static (since all AI tanks are aiming to a non moving Player tank).
I am stuck at this point. Would really appreciate any help. Thanks!