Useful log function

I noticed at this point in the lecture you can easily return the distance in meters from the tank to where you are pointing out.

This is useful and fun information in general and is easy to implement or even store for future uses or tests (I mean, there isnt even a ruler tool in UE4)

Using the current information you can easily log this information as opposed to the location of where the tank is aiming.
To do this simply log out the size.

 FVector HitLocation;
 if (GetSightRayHitLocation(HitLocation)) { // remember this is a function implimented in this section
	 GetControlledTank()->AimAt(HitLocation); //<- this function does not change the HitLocation (its from the lecture)
	 FVector CurrentLocation = PlayerCameraManager->GetCameraLocation();
	 FVector Difference = HitLocation - CurrentLocation;
	 float MetersAway = Difference.Size()/100;
	 UE_LOG(LogTemp, Warning, TEXT("Meters Away: %f"), MetersAway)
 }

You could also log it with the hit location if you wanted ofcourse. Don’t forget that FVector.Size() returns a float(the magnitude); I forgot this at first and it tripped me up xD

Oh, BTW, this reports the meters distance from the camera, not the tank. Slightly confusing, but you would need to get the distance from the tank or tank mesh then calculate from there for that…

You can do this by changing the current location caculation value to this:

FVector CurrentLocation = GetPawn()->GetActorLocation();

You can also concurrently print out the value in feet by multiplying times 3.28084, as so:

UE_LOG(LogTemp, Warning, TEXT("Meters Away: %f  Feet Away: %f"), MetersAway, (MetersAway*3.28084))

Like so together in the same code:

// revised version:
 void ATankPlayerController::AimTowardsCrosshair() {

	 if (!GetControlledTank()) { return; }

	 FVector HitLocation;
	 if (GetSightRayHitLocation(HitLocation)) {
		 GetControlledTank()->AimAt(HitLocation);
		 FVector CurrentLocation = GetPawn()->GetActorLocation(); // changed value
		 FVector Difference = HitLocation - CurrentLocation;
		 float MetersAway = Difference.Size()/100;
		 UE_LOG(LogTemp, Warning, TEXT("Meters Away: %f  Feet Away: %f"), MetersAway, (MetersAway*3.28084))
	 }


	 return;
}

You could also go the whole shibang and print all values:

	 float MetersAway = (Difference.Size()/100);
	 float KiloMetersAway = (MetersAway / 1000);
	 float FeetAway = (MetersAway*3.28084);
	 float MilesAway = (FeetAway*0.000189394);
	 UE_LOG(LogTemp, Warning, TEXT("Distance Away:| Meters: %f| |Feet: %f| |Miles: %f| |Kilometers: %f "),
						MetersAway,   FeetAway,	 MilesAway,  KiloMetersAway)

This is actually more useful for the context of this video:

void ATankPawn::AimAt(FVector HitLocation) {
	FVector CurrentLocation = GetActorLocation();
	FVector Difference = HitLocation - CurrentLocation;
	float MetersAway = (Difference.Size() / 100);
	UE_LOG(LogTemp,Warning,TEXT("%s is aiming at %s, Distance: %f meters"), *GetName(), *HitLocation.ToString(), MetersAway)

	return;
}


^ Here we have it with other tanks involved. The distance makes it easy to vertify what tanks are aiming at you.

Privacy & Terms