IsCrosshairLocked function to replace IsBarrelMoving

Hello,

Hello, the IsBarrelMoving function uses the FVector.Equals that does not work when the AimDirection Z Axis is less than 0, because the AimDirection Z Axis can get negative values but the Barrel Z Axis minimum value is 0.
The IsCrosshairLocked function only validates the AimDirection Z Axis when the Barrel Z Axis is greater than the tolerance value.

AimDirection is a class member variable, in my architecture I’m using the Player Controller to handle the aiming, in your case, you need to use the AimingComponent (UTankAimingComponent).

bool ATankPlayerController::IsCrosshairLocked(float Tolerance)
{
    FVector BarrelDirection = ControlledTank->GetBarrelComponent()->GetForwardVector();
    bool bValidateLockX = FMath::Abs(BarrelDirection.X - AimDirection.X) <= Tolerance;
    bool bValidateLockY = FMath::Abs(BarrelDirection.Y - AimDirection.Y) <= Tolerance;
    bool bValidateLockZ = (FMath::Abs(BarrelDirection.Z) > Tolerance) ? FMath::Abs(BarrelDirection.Z - AimDirection.Z) <= Tolerance : true;
    return (bValidateLockX && bValidateLockY && bValidateLockZ);
}
1 Like

Privacy & Terms