Unreal Engine C++ Course | ENG
  • About course
  • Links
    • Git
    • Visual Studio
    • Unreal Engine
  • Unreal Editor Hotkeys
  • Visual Studio Hotkeys
  • Console commands
  • Problems and solutions
    • Unable to start program error
  • Lessons
    • Lecture 065
      • Additive animation upon landing
      • Blocking shooting while running
    • Lecture 079
      • Pickup is visible after the ammo has been taken
      • Ammo is not picked up if character spends ammo at the pickup respawn point
    • Lecture 089
      • NiagaraSystem does not attach to muzzle
    • Lecture 148
    • Lecture 155
  • UE5
  • Automation
    • Code formatting
  • Student projects
Powered by GitBook
On this page

Was this helpful?

  1. Lessons
  2. Lecture 065

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

PreviousAdditive animation upon landingNextLecture 079

Last updated 3 years ago

Was this helpful?

Commit in repository