Unreal 5 C++ – Warrior Game Mechanics


A third-person warrior game built in Unreal Engine 5 with C++ and Blueprints. The project covers a broad set of gameplay systems implemented entirely in C++: character movement and stamina management, melee combat with weapon equipping, AI-driven enemies, a variety of pickups and interactive world objects, UMG-based HUD, and a full save/load system. Blueprint subclassing is used exclusively for asset configuration and designer-facing event implementations.


The Player Character is built on ACharacter with a spring arm and camera setup. Movement input drives a four-state stamina machine — Normal, BelowMinimum, Exhausted, and ExhaustedRecovering — that controls whether sprinting is allowed and how fast stamina drains or recovers each frame. Attacking plays one of two randomly selected sections of a combat montage, and auto-focus interpolates the character’s rotation toward the active combat target during the attack.


The Weapon System is built around AWeapon, which extends the base AItem class:

  • Equipping attaches the weapon’s skeletal mesh to the character’s RightHandSocket via USkeletalMeshSocket, disables idle particles, and sets the weapon instigator to the player’s controller for kill attribution.
  • A UBoxComponent (CombatCollision) is enabled and disabled from anim notifies during the attack montage. On overlap with an enemy it applies damage via UGameplayStatics::ApplyDamage and spawns hit particles at the WeaponSocket.
  • EWeaponState tracks whether the weapon is lying in the world waiting to be picked up or is currently equipped. Multiple weapon types are supported through Blueprint subclasses.

The Enemy AI is driven by AAIController and two detection spheres on AEnemy:

  • A large agro sphere (600 units) triggers MoveTo when the player enters, and stops movement when the player leaves.
  • A smaller combat sphere (75 units) schedules attack montages with a randomized timer between AttackMinTime and AttackMaxTime. A UBoxComponent attached to EnemyWeaponSocket handles hit detection against the player during the montage.
  • On death, all collision volumes are disabled, the death montage plays, the mesh is frozen via bPauseAnims, and the actor is destroyed after a configurable delay. The player’s combat target is updated immediately so the HUD health bar hides correctly.

The Item Hierarchy provides a composable base for all world pickups:

  • AItem manages a sphere collision root, idle yaw rotation, overlap particles, and sound. Subclasses override OnCollisionVolumeBeginOverlap to define pickup behavior.
  • APickupItem fires a BlueprintImplementableEvent (OnPickup) on player contact and immediately destroys itself, letting designers implement the effect — health restoration, coin increment — entirely in Blueprint.
  • AExplosiveItem applies direct damage to both the player and enemies on overlap using UGameplayStatics::ApplyDamage, triggers the parent’s overlap particles, then destroys itself.

The Floor Switch and Floating Platform cover interactive world mechanics:

  • AFloorSwitch raises a door when the player steps on a trigger box and closes it after DoorSwitchTime seconds once the player leaves. RaiseDoor, LowerDoor, RaiseDoorSwitch, and LowerDoorSwitch are BlueprintImplementableEvents driven by Blueprint timelines, with UpdateDoorLocation and UpdateDoorSwitchLocation as C++ callbacks.
  • AFloatingPlatform lerps between StartLocation and EndLocation using FMath::VInterpTo. EndLocation is authored in local space via an editor widget handle and converted to world space at BeginPlay. The platform pauses at each endpoint for InterpTime seconds before reversing.

The Spawn Volume (ASpawnVolume) is a box-shaped actor that randomly selects a class from a designer-configured array and places it at a random point within its extent. If the spawned actor is an AEnemy, SpawnDefaultController is called manually and the resulting AAIController is wired to the enemy — necessary because characters spawned mid-game via SpawnActor do not have their controller created automatically. SpawnOurActor is a BlueprintNativeEvent so Blueprint subclasses can override the spawn logic.


The Save/Load System serializes full character state into UMainSaveGame:

  • Saved data includes health, stamina, coin count, world transform, and the equipped weapon’s name.
  • On load, the weapon is restored by looking up its name in an AItemStorage actor — a designer-configured TMap<FString, TSubclassOf<AWeapon>> placed in the level — spawning the class and equipping it to the character.
  • LoadGameNoSwitch restores stats in-place without triggering a level transition. LoadGame additionally calls SwitchLevel if the saved level name differs from the current map.

The HUD and Player Controller (AMainPlayerController) manage three UMG widgets created at BeginPlay: the main overlay, the enemy health bar, and the pause menu. The enemy health bar is projected from the combat target’s world position to screen space each tick via ProjectWorldLocationToScreen, avoiding a widget component on the enemy actor. The pause menu toggles FInputModeGameAndUI and FInputModeGameOnly along with cursor visibility. DisplayPauseMenu and HidePauseMenu are BlueprintNativeEvents for Blueprint override.


GitHub Repository

Demo

Learning Source

Leave a comment

Create a website or blog at WordPress.com

Up ↑