2D MELEE COMBAT in Unreal Engine 5 - Beginner Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
i'll show you how to implement simple 2d sprite combat in android engine 5. but this method can also be applied to 2d 3d hybrid games as the starting point i've prepared a stage from a tile map and a samurai based on the paper character class if you want to follow along but don't know how to set up a basic 2d project in the first place you should probably check out this video first a link to the free assets i'm using can be found in the video description now let's get started with the combat mechanics open up the samurai blueprint create an event for the space bar when it's pressed we want to set a different flipbook on our sprite during the preparation i created a flipbook for idle attack taking a hit and dying and social view for your character here we want to set the flipbook for attacking when pressing space now the character will play the attack animation however it is looping set looping needs to be called on the sprite with new looping being unticked now the animation will play out once and stop but we don't go back into our idle state on the sprite we can scroll all the way down to the events and click plus for on finish playing this will trigger every time a non-looping flipbook finished playing its animation so this is the perfect timing to set the samurai back to the idle flipbook and also enable looping again on the sprite but since the sprite stopped playing already we need to restart it we can check if the sprite is currently playing or not and only call play on it if it is now this is working perfectly we can select the notes for setting the looping to true and restarting the sprite and collapse them to a function to keep things more organized now it's time to create a hitbox for this we'll attach a box collision to our character and call it hitbox to see how far our attack reaches we can temporarily set the source flipbook of our sprite to the attack animation and the play rate to 0.1 now we can adjust the box extent and location of our hitbox to match the animation you can see that the hitbox also has some depth to it this becomes important if you make a beat em up that has multiple lanes so you can set how far your attacks reach into the other lanes set the sprite back to the idle animation and the play rate to 1.0 for the hitbox we want to set it up so it can only hit characters and not other objects scroll down to the collision settings set collision preset to custom and set everything to ignore at first then set only pawn to overlap create a new function called hit check we'll use this to check who is in our hitbox and apply damage to them from the hitbox we can get overlapping actors and set the class filter to the samurai class [Music] we then need to use a for each loop to iterate over the array of actors this hitbox will also overlap with ourselves so we need to make sure to only proceed applying damage if it's another samurai we can then call apply damage on the array element [Music] and set up a variable for the damage amount or samurai deals with each attack set up a boolean variable called is attacking we want to set this to true whenever the attack button is pressed and to false whenever flipbook animation is finished playing [Music] on the tick event we can then add a branch that only executes when is attacking is true [Music] and for now call hit check from that but as you can see there's still nothing happening we can set is hidden in game to false on the hitbox when is attacking gets activated for visualization purposes [Music] [Music] and set it to true when is attacking is set back to false you can now see an outline over hitbox whenever we are checking for hits but the other character is a bit too far so let's move it closer we then also want to add the any damage event here which is triggered on the receiver of apply damage for now we'll just print out the received damage as you can see we're getting a lot of locks for a single attack this is because for now we're calling hitchhike once for each frame for the entire time the attack animation is active we only want to check this on selected frames of the animation we can take a look at the attack flipbook to see which frame is best suited as an attack trigger in this case we want frame number 3 to be the trigger we can get the playback position in frames from the sprite to tell us what animation frame we are currently on let's only execute hitchhike if this is equal to 3. [Music] you might expect the lock to only show up once now but it's still showing up four times per attack that's a lot less than before but still more than we want this is related to the flipbook being set to 15 frames per second while the game is running at 60fps this basically means that each frame of animation is actually active in the game for 4 consecutive frames we want to place a do once note before calling the hit check to only allow to trigger once [Music] to visualize things we can bring down our note which sets the visibility of the hitbox down here we unhide it when checking for hits but hide it on every other frame you can see that the log now only shows up once which is great but it's also only showing up for the first time we attack let's create a custom event to reset the do once node i'll call it reset attack we then want to call this from on playback finish to reset this between animations now the print string is showing up once per attack which is exactly what we want playing a particle effect will give the attack some more flair open up the third frame of the attack sprite and add a socket we can then position it on the location where we want to spawn the particle effect the socket should be called hit pause we want to create a local variable called hit something which will default to false [Music] when we find at least one enemy to apply damage to we set this to true after the loop is completed we can set up a branch using this variable only if it is true do we want to spawn an emitter at location i'll use a yellow spark effect we can get the socket location from the sprite by typing in the socket name of hit pause you can now see a parting effect being spawned whenever we hit something while we're at it we might as well just add some sound effects as well here but obs decided not to record my audio while filming this let's whip up a quick health system create a float variable called health and set the default value to 200. [Music] whenever any damage occurs we want to subtract the damage from our health and set that as the new health [Music] we then check if our health is smaller or equal to zero and create a branch [Music] [Music] if we have no health left a death animation will play [Music] if the character does have health left it'll play a get hit animation instead in both cases we want to set the flipbook to not looping [Music] but if the character dies it currently goes back to the idle animation we can create a variable called is that and set this to true when the death animation plays [Music] this can be used to not execute anything after the unfinished playing event in case the character has died [Music] now we have a fully functioning melee combat system but there are many quick and easy ways to make huge improvements to this in 3d games adding camera shake slow downs or hit stop improves the feel of combat 10-fold and this can also easily be applied to 2d combat as well i already made an in-depth tutorial about hit stop which you can check out here [Music]
Info
Channel: Cobra Code
Views: 64,210
Rating: undefined out of 5
Keywords: game development, unreal engine, gamedev, ue4, ue5, game dev, 2D combat, 2D combat Unreal Engine, combat unreal engine, unreal engine melee combat, unreal engine 2d melee combat, unreal engine 5 combat, ue5 melee combat, ue5 combat, paper 2d combat, unreal engine paper 2d, 2d combat tutorial, 2d melee combat tutorial, unreal engine combat tutorial, unreal engine hitbox, unreal engine 5 hitbox, unreal engine hit detection, combat hitbox tutorial, hitbox tutorial
Id: JCdRe4kC3FM
Channel Id: undefined
Length: 10min 57sec (657 seconds)
Published: Wed Aug 03 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.