Just a different approach that, as far as I can tell, works as well as the one used in the lecture. The relevant bits:
UTankAimingComponent::UTankAimingComponent()
{
PrimaryComponentTick.bCanEverTick = true;
FiringStatus = EFiringStatus::Reloading;
LaunchSpeed = 5000.f;
ReloadTimeInSeconds = 3.f;
}
void UTankAimingComponent::BeginPlay()
{
Super::BeginPlay();
LastTimeFired = FPlatformTime::Seconds();
}
void UTankAimingComponent::TickComponent(float DeltaTime, enum ELevelTick TickType,
FActorComponentTickFunction *ThisTickFunction)
{
if ((FPlatformTime::Seconds() - LastTimeFired) <= ReloadTimeInSeconds)
{ FiringStatus = EFiringStatus::Reloading; }
}
void UTankAimingComponent::MoveBarrelTowards(FVector AimDirection)
{
if (!ensure(Barrel && Turret))
{ return; }
auto BarrelRotator = Barrel->GetForwardVector().Rotation();
auto AimRotator = AimDirection.Rotation();
auto DeltaRotator = AimRotator - BarrelRotator;
if (DeltaRotator.IsNearlyZero(0.03f))
{
FiringStatus = EFiringStatus::Locked;
return;
}
else
{
FiringStatus = EFiringStatus::Aiming;
Barrel->Elevate(DeltaRotator.Pitch);
Turret->Rotate(DeltaRotator.Yaw);
}
}