Ever since I have been working part-time, my free time has greatly diminished. After about a week of clerk work, today was a day I can finally once again train in the ways of Unreal Engine 5.
I got the health; I got the damage; you want a health bar. And a health bar you can make in a Widget Blueprint. You can imagine the logic is similar to physical drawing since everything is stored in a framework named a Canvas Panel. The basic process goes as such:
From the Palette tab, create a Canvas Panel (Canvas for short).
Drag a Progress Bar (see slide 1) into the Canvas.
While selecting it, there will be a flower-looking icon on the top left of your Canvas. The petal things are scaling anchors that rescale Widget elements with scaling screens.
In my case, it was most convenient to drag the Southeast one to the bottom left of the Bar then tidy up the other corners alongside said bar's remaining vertexes.
Create a Binding to connect the player variable Health with the Progress Bar. Creators will be redirected to a parallel node-centric mode (see slide 2).
Because the Bar goes through a value range between 0 to 1 rather than our character's higher range of 0 to 50, I needed to divide the current Health value by its max value (50) for the Bar to function as desired.
Plug a Create Widget node to the Add Mapping Context node back at the player character's Event Graph.
Set the class to the health bar Blueprint.
Connect a Get Player Controller variable node to the owner slot of the Create Widget node.
Connect a Add to Viewport node from right to the Create Widget node. This final node directly commands the system to display the health bar UI in front of the camera viewport.
Enemy health bars required a different approach to Widget creations since I envisioned them to simply float atop enemy NPCs' heads, directly facing the player character.
In stark difference to the 3rd-person health bar UI style, the enemy health bar is positioned in the middle of the Canvas. The Binding formula (Health variable value / Health static max value) is identical to the player's Binding, but the fetch node structure is not (see slide 2). Get Actor Class nodes fetch Actors (enemy Blueprint) from Arrays by design. Simply connect the GET Array end to the Cast to Enemy Blueprint node's Object reference slot so the system accurately references the current enemy Health value.
Heading to the Event Graph (see slide 3), I made a new node set that automatically turns the Widget in (3D) World space towards the player. Logically, the Find Look at Rotation node fetches the location of the Widget and player Actor. It then relays the Widget's world rotation data to the Set World Rotation node which instructs the Widget to the target player.
After the above, go to the enemy Blueprint Viewport (see slide 4). Add a Widget displaying component, then drag it to wherever location you want it to generate on.
Upon testing, a key issue I had with this iteration of the health bar is its rotative properties making it invisible at flat 90-degree angles.
Deciding that 3D Widgets were not worth the hassle, I set it to a Screen Space, effectively making it 2D and more importantly not shortly disappearing from view due to close distance.
With the player-facing Widget functional, my next concern took me to the enemy death mechanic. Post-ragdolling, the collision capsules of the corpse will continue to damage the player upon contact. The solution? Connect simple conditional branch nodes to each living action pre-execution. Plus a cease-all-movement node to the death node cluster.
BAM! Now the enemy can't hurt you if they're dead. The lingering collision capsule does sound like an appealing mechanic for a challenging MMORPG instance though, so I am noting down the idea for later.
A peculiar event thing while experimenting with the Set World Rotation-era Widget was after my player gets killed before killing an enemy, a long list of runtime errors seemingly related to the Set World Rotation node. NOT upon any player death; a player death BEFORE an enemy dies. Upon Widget 3D-to-2D conversion, the errors passed onto nonexistence with the node.
For the foundational touches, mesh up the platforms, put two more enemies on the non-spawn point cubes, and the basics for my 3rd-person shooting game are complete! Now if only I could individualize the enemy health bars…