Blocking shooting while running

The ability to shoot while running looks unrealistic due to our running animation, so it's best to disable shooting while running.

I highly recommend that you first try to implement this logic by yourself, as a homework task.

Improvement can be done after Lecture 065 or at the very end of the course.

Solution

  • Add a new flag to the base weapon class FireInProgress

void ASTUBaseWeapon::StartFire()
{
    FireInProgress = true;
}

void ASTUBaseWeapon::StopFire()
{
    FireInProgress = false;
}

bool ASTUBaseWeapon::IsFiring() const
{
    return FireInProgress;
}
  • Also add a function to the weapon component:

bool USTUWeaponComponent::IsFiring() const 
{ 
    return CurrentWeapon && CurrentWeapon->IsFiring(); 
}
  • In the character class, we create the OnStartFire function and bind to it when firing is started:

PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &ASTUPlayerCharacter::OnStartFire);
  • After that, add the code for the OnStartFire function and update the other two functions:

void ASTUPlayerCharacter::OnStartFire()
{
    if (IsRunning()) return;
    WeaponComponent->StartFire();
}

...

void ASTUPlayerCharacter::OnStartRunning()
{
    WantsToRun = true;
    if (IsRunning())
    {
        WeaponComponent->StopFire();
    }
}

...

void ASTUPlayerCharacter::MoveForward(float Amount)
{
    IsMovingForward = Amount > 0.0f;
    if (Amount == 0.0f) return;
    AddMovementInput(GetActorForwardVector(), Amount);

    if (IsRunning() && WeaponComponent->IsFiring())
    {
        WeaponComponent->StopFire();
    }
}
  • Additional details with calling parent functions StartFire and StopFire can be found in the repository

Commit in repository

Last updated

Was this helpful?