How to Shoot Projectiles | Unreal Engine 5 Tutorial (Arrows, Spells, Bullets & more) | ue5

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everyone and welcome to another Unreal Engine 5 tutorial in this tutorial we're going to be creating a projectile system so I'm going to show you how to create multiple different types of project styles that you can shoot like an arrow magic spells bombs bullets really anything that fits in your game and we're going to be combining it with our damage system tutorial from last time to create something that looks like this okay so let's get started alright so I'm starting here with a blank project um because I don't need anything from the first or third person or anything like that but I'm going to enable the starter contents so once you create a blank project you see this open world also two things to note is I'm also using the Infinity Blade and Paragon Gideon assets so these are free on the asset store because they have some cool effects that that I want to use I'm going to leave a link down in the description if you want to use them for your own project as well all right so uh back in our editor uh now the first thing we want to do is we want to create a demo map because this open world is just too big um so go here and create new level create a basic level uh I'm going to save this in a new folder called maps and let's just call this demo all right next up uh we want to create a new folder called projectiles and this is where it's going to hold everything related to our projectiles and the first thing we want to create is a blueprint class of type actor and call this BP projectile base now why did I call it base because it's going to be a base class it's going to be a parent class for all other project tiles to inherit from so it's going to contain everything that's common between every different type of projectile so first thing to do is first thing to do is see what's going to be common for all projectiles so over here on the left in our components let's add a box Collision um yeah just call it box Collision I spell that right yeah make sure to drag this onto your default scene root so that this Collision is now your new default uh route next thing we want to add is a static mesh now not all projectiles will need a mesh but we can always disable it in a child's class and we'll call for now let's add just a I don't know a sphere as our test mesh I'm going to make this a bit smaller oh and disable this um and we want our box extends to match our mesh or whatever we add here so let me just change the perspective here to front go to our box collision and modify the extents so that they match perfectly with our sphere and let's go over here to the left and edit that as well all right so this looks nice now for this to be a project style it has to move of course and what makes projectiles move is a component called projectile movements components now this projectile movement component allows you to edit things like speeds uh gravity scale bounciness friction and so on for now we just care about the initial speed max speed and gravity um so if I click simulate what happens now is this just Falls and the reason this happens is because our gravity is set to one if I set gravity to zero that means I'm disabling gravity now simulates nothing happens because we don't have any speed so let's add like a default speeds for both initial and Max Speed 500 now if I click simulation it's moving great um I can also take this and put it into our world here now when I click play oh it starts moving as well but you'll notice that in the world it's hard to see which direction is going to start moving in so we can go back here and add a arrow and this arrow is just something for the editor it doesn't appear in game and it just used to tell you like which direction this is moving in so yeah here now it's a lot clearer and if I click play oh there's some stuttering going on but yeah you can see it moving um all right so what else do we need for our uh projectile now maybe we want this uh these default speed that we added and the gravity as well to be editable per instance what does that mean means that if I have maybe two of these maybe I want this one to go at 500 this one to go at a thousand or different speeds so let's add some variables that we can manipulate per instance so over here on the left let's add one variable called speed that's going to be of type float let's add another variable called gravity it's also going to be of type float you can also limit the value of gravity to say between 0 and 1 here in this value range and you can also put it here in the slider if you want it to be a slider now compile and save and add maybe a default value for Speed now let's use these variables over here in the construction script pull from our projectile movement and say set initial speed and that's going to be our speed variable and we can also set max speed I'll just clean clean this up a bit and that's also going to be our speed variable and finally sets gravity and that's going to be our gravity now you want them both you want all of these three to be instance editable and expose on spawn so that we can edit it per instance and also our speed and synthetical exposed on spawn and one nice thing I like to do when adding any variable is changing the category so for our speed let's create um well let's create a new category call it projectile settings and let's use this category for our speed and our gravity and basically what category does is that it creates a new tab over here to organize your variables and also you'll notice here oh I have to compile you'll notice here that you have projectile settings as a category as well nice so let's edit this one to be a thousand and this one to be 500. now if I press play nice this one is going a lot faster than the other one that's what we want great um so one other thing to note is um let me just delete one of them so if I create a if I just add a cube here let's move it here let's just make a wall so if I press play now you'll notice that the projectile just goes right through um our wall now for my for my game I want I want it to be blocked by objects so I'm going to edit the Collision here to be block all Dynamic instead of overlap all now depending on your game maybe you want projectiles to overlap uh if you have like a Dodge or something like that where you can dodge away from projectiles then maybe you want to do overlap for my purposes block is better and also for the mesh go here and say no Collision because we don't want Collision on both if we do block compile and save now if I go back here and play it just hits the wall and stops well great so that's what we want now let's handle what happens when we collide so if I go over here on my box Collision scroll down I can click on on component hit and now I want to Define what happens when any base projectile collides with something the first thing I want to do is dispatch an event so this tells any class that spawns a projectile when a hit event occurred so go down here to event dispatchers let's call it on projectile impact or whatever you want to call it and this is going to be called as soon as an impact happens and we want to pass a few variables as well we want to pass this hit and we also want to pass the actor that it hit with so to do that let's go back to our event dispatcher and then inputs let's add two inputs first one call it oh call it other actor and make this of type actor object reference and the other one let's call it hit and this will be of type hit results great now we can just pass them here what else do we want to happen when we Collide for now I wanted to explode or disappear or whatever so I'm just going to call destroy actor obviously you want some effects to be added here or sounds but we're going to do that just in a few seconds but let's test this out and see now if I press play it hit the wall and disappeared you didn't see that it was a bit too fast so if I play again hits the wall and is destroyed immediately nice so for now this is good um uh this is enough for our base projectile let's start building some child's projectiles as well all right so back here in our content drawer just right click your base project tile and say create child blueprint class and let's call this one VP projectile Dark Energy because I'm going to create a dark energy projectile open it up and now let's edit our components so first of all I don't need the mesh for this one so I'm going to go here and say clear but what I do need is a particle system because these are the effects that I'm using from the Gideon asset pack so I'm gonna go here select particle system make this a child of box collision and search here for Gideon Project Style so this is what it looks like and also I want to just modify the extents of this box a bit make it a bit longer and move this projectile system just here and move the arrow alright so this is what it looks like if I take this and put it in my world it's gonna delete this one it's going to behave exactly like the base project tile because it inherits all the functionality from it so it hits the wall and is destroyed all right so this is how you can create multiple different project styles using the same Base Class so let's see how we can edit different sounds or different effects per projectile so back here in my base class before we destroy the actor we want to play we want to spawn a particle effect again if you have Niagara system please make sure to use Niagara effects because yeah that's the new way of doing VFX but the ones I have are old so that's why I'm using particle system so here I'm going to say spawn emitter at location and the asset that I'm going to be using is also from the Gideon asset pack called burden hit world and the location here so if I pull out of the hits and I click break hit results I have a location this is where the hits occurred um so now if I play you'll see that it spawns this effect again play here there's an effect the problem is I did this in the base project tile that means all the projectiles are going to be using the same effects and of course we don't want that so I'm gonna right click promote this to a variable call it impact effects add a new category for it call it effects category compile I'm not going to give it any default so it's not going to have a default value but it's going to be added by our dark energy so here if I click on the base of the actor I'm going to find the effects tab now I can add my hit World effect so this will add it for this projectile but not any other ones that I create nice so we also want the sound because I'm not hearing any sounds so once an impact happen let's call play sound at location and again same thing here this sound promotes variable call it impact sound add it in the category called sounds and the location will just be the same location well all right all right so uh now let's go back to our call First compile go back to our dark energy project tile and let's add this impact sound to be um what was it called explosion and this explosion sounds like this and it comes with the starter pack now let's see what that looks like play one more time very nice so let's create um yeah so this is basically it for this dark energy projectile now let's create another one um I can either duplicate this one or right click create blueprint class whichever you like so I'm going to create a child here call this BP projectile Arrow I'll actually call it fire arrow because it's a gonna be a fire arrow all right open this up um now I don't have a arrow mesh so I'm just going to use um I don't know some Cube and I'm gonna scale this cube a bit um with eight zero points zero one yeah so this is going to be my arrow uh and of course we need to edit the box extents as well to fit the arrow well editing the wrong one yep and go to the front oh actually this is the other Arrow um yeah something like that doesn't have to be perfect so this is good but I also want to add some trail effect behind this to make it look like a fire arrow so I'm going to add also particle system and call this Trail make it bit to the back and this one is from the Infinity Blade asset pack it's called Fireball base Trail um and also don't forget to edit your impact effect and impact sound so the impact effect that I want is an explosion again this is from the starter pack and the impact sound let's just use explosion again all right so let me come here and add this Arrow move it a bit back so you see what it looks like all right not sure if you were able to see that let me play again yeah so they're both playing the same both playing the same sound right now but different effects and of course I can still control the speed the gravity so if I move this a bit up like here oh I'm moving both of them that's fine uh so if I edit the gravity for this one say it's 0.3 and the speed is a thousand let's see what that looks like well I made the speed 100 not a thousand okay now it's a thousand so you can see that it falls down because there's some gravity to it um one thing you can also do is uh so you notice that it's not changing the rotation of the arrow while it's falling so to change the rotation you go here and search for rotate and click on rotation follows velocity now again if I play here that looks more like an arrow now nice um I'm going to create one more projectile and then we will add some more functionality to them so I'm going to right click create child blueprint class call this one projectile ice so I'm gonna go here um again don't need a mesh for this one so I'm just going to clear it and I'm going to add a particle system make it a child of the Box collision and this one I'm going to use Elemental ice projectile and that's what it looks like and this is also from the Infinity Blade effects asset pack uh huh uh edit the Box Collision extents um something like that let me move the arrow here and go to the front yeah something like that so these are the extents of this one uh and for the sounds uh well for the effect I have some ice explosion effect again from the Infinity Blade effects and the sound I heard a sound that came with the engine that sounded um found it nice it was possessed player yeah this one so it sounds sounds like an ice explosion I'm just gonna use this one uh again this just for a demo so it doesn't really matter and let's put in our Ice Project Style here and see what it looks like well again some stutter that looks nice lots of projectiles great now what we want to add is a targeting system so right now you can see the projectile they just move um straight so if I rotate this one for example and I play then it's going to keep moving where in the direction it was pointing at but what if we wanted to move to find the targets and go to that Target not keep homing around the target but just find its location or initial location go to it so to do that let's add a Target variable to our base here so I'm going to go to the variables and a variable called Target that's going to be of type actor and object reference let's add a new category for it call it Target and this is going to also be instance editable and expose on spawn because we wanted the target to be different per uh instance of this uh component um all right so let's actually create an like a dummy enemy for this so if I go here let me create a blueprint of type um I don't know character call it BP enemy open this up I'm not sure if we have a mesh oh sorry I'm not sure if we have a mesh and yep we have this skeletal mesh we can use this one that's fine let's rotate it to face the arrow and mm-hmm right now let's add this uh enemy to our scene let's move it here so right now maybe I want this um this ice projectile to find the enemy's location and go to it to do that I'm going to first check uh in our um begin play if we have a valid Target so I'm going to get Target right click convert to validated get so if the target is valid we want to rotate don't need these I'm going to delete them I'm going to create a new custom vent and call it rotate to Target and if it's valid I'm just going to call this event rotate to Target now an important thing to know is that you can't just change the rotation of your actor so I can't say something like set actor rotation because the projectile movements if you hover here you can read that projectile movement component updates the position of another component so that means that anything that I change here rotation or location will be overridden by the projectile movement so actually what we want to do is set the velocity of projectile movement because velocity is speed and direction so how do we get the direction well there is a function called get unit Direction and it takes a from vector and a two Vector so our from Vector is get the location from our current actor location to our targets location so get Target get actor location and that's R2 vector now this is just a unit Vector so it's only Direction We Now mono multiply it by our speed so this gives us velocity direction time speed now if we have a valid Target we're going to rotate towards that Target as soon as we begin play let's see what that looks like so this um this Ice Project tile let's point it upwards and play it obviously is going to go upwards now let's keep the rotation the same but give it a Target so I can click on this pick actor from scene and just click the enemy now the target is my enemy now if I press play it found my enemy and it went to it so it changed its default its initial uh look rotation nice um all right so now if I actually move this enemy it's not gonna follow it so let me click simulate oh it was too fast so now sorry let me play again see if I'm moving the enemy this is still moving in its current uh Direction because it only sets its rotation on begin play and doesn't keep updating it as its targets moving so what if we do want it to be sort of a homing projectile that follows its Target well for that uh in our projectile Movement we have an homing Boolean so if we set is homing to true we can pass in a Target to follow so let's do that in our construction scripts where we set all of our projectile movement variables let's also check first um all right we can get our targets and convert to a validated get so if we have a Target and let's add a new variable call it is homing because maybe you want a Target but you don't want it to be homing and this is going to be a Boolean we can add it in the projectile settings category so if we have a Target and create a branch here and is homing is set to true then we want to get our well we want to get our project style settings here and set is homing projectile true as well tie these up uh one thing we also want to do is modify this homing acceleration magnitude so this is the velocity of at which the projectile moves when its Target moves so at zero that means not going to move so let's set it to something like uh I don't know 2000. and don't forget is homing also should be instance editable and expose on spawn now let me click my ice pick here again and set is homing to true great now if I simulate and move oh it's not homing why is it not homing let's see um if targets is valid and is homing is true then sets is homing for oh we forgot to set the Homing targets so we also have to set homing Target component yeah so the target here is not an actor it's a scene component so we can get our targets actor and get its root because the root is always a scene now it should work let's try one more time yeah it's following me it's following me and it hit me great all right so um I created something called a projectile shooter here real quick um this is just a um I don't know like a like a wall that spawns projectiles so all it does is that every um two seconds or every frequency it spawns a projectile that we Define here in the projectile types so we can select any one of our project tiles and you can set its speed gravity and so on now the reason I did that is to demo something really important so let's put our projectile shooter somewhere in the world um let's delete all these other project tiles we don't need them and also Let's uh so now if I press play It's supposed to spawn a projectile let's see what happens and nothing's happening because I didn't select a projectile type let's say I don't know fire arrow now you can see that it's spawning but exploding right away because of course when it spawns it collides with the spawner itself now this will happen if maybe you have a player that's spawning an arrow or projectile it's of course going to collide with the player themselves first so we want to prevent that from happening uh to do that back in our projectile base here on beginplay we're going to call something called ignore actor while moving and this is going to be on the box Collision now ignore actor while moving set this to True basically takes an actor here and it disables collision with that actor so the actor here is going to be um whomever spawned the projectile so if we go back to our productile shooter here you'll see that it has an owner and this owner is a default whenever you spawn an actor from a class so spawn actor from class here it will always have an owner so I'm going to set my owner here as self which is a projectile shooter components and that way here I can just say get owner compile and save now if I go back here and we're still colliding with the owner why so box Collision ignore actor while moving Target is owner um uh owner here is self I am missing something oh this is only called when Target is valid so let's actually move this uh to be behind so it's going to be the first thing it does and then checks if Target is valid because obviously if Target's not valid it wasn't going to go here uh okay let's try this again there we go now it's spawning our arrows uh huh great so I can also change this type to be I don't know Dark Energy make it spawn every second and you can see that there there it goes and of course here I can also change the target to I don't know get actor uh get actor of class enemy I'll just plug this in here just doing it very dirty now to see yeah this is how you add a Target and now it's going to the right target perfect Next Step I'm going to do something exciting if you followed my last tutorial you'll see that I created a damage system that had a player and an enemy and they can damage each other now I'm going to take this projectile system and I'm going to integrate it with our damage system to see how you can actually spawn project tiles from a player to an enemy or vice versa so first thing to know if you want to move files from one project to another I can right click here on this projectiles folder and say migrate now this is going to see all of the dependencies that this uh that these classes have so it's depending on the Infinity Blade Paragon blah blah and it's going to migrate them to a different project for example the other projects already has starter content so I'm not going to choose that one I don't want the enemy for example that was just for show uh you can also say yeah me I don't want this projectile shooter it was also a demo so yeah you got the idea you can choose what you want then if you click OK and it's going to ask you to find the content folder of the where you want to put it so if I go here find damage system tutorial then I can select the content folder now I've already done that so I'm not going to do it again but if I go over here to my damage system by the way let me close these because it's uh slowing things down so if I go over here to my damage system I can see that I have my project files folder with all the things that I created so the damage system tutorial just covered how to create an enemy and the player and the player and the enemy can damage each other like this and then they can kill each other as well and I can heal and things like that but the attacking was just done by doing a sphere Trace as you can see here when I click it just does a sphere Trace now I'm going to replace this sphere Trace with spawning a projectile and see how we can actually start damaging the enemy so I'm gonna go over here to my um damage system to my player class and over here when I click the left Mouse button I do the sphere and then if the sphere hits something I call take damage on that's something that I hit now instead of doing a sphere Trace I'm actually going to spawn a projectile so I'm going to delete all of this and just here I'm going to say spawn actor from class and this class is going to be our project tile now I can select any one of these project tiles but I want it to be a random selection so to do that I'm going to go here and pull a select node and I'll just refresh this to remove the errors and in the select node I'm going to select the first option to be our projectile Arrow second one would be our um dark energy and the third one will be our ice so we have three options here to select from and the index will Define which one will be selected so index 0 will select this one one and so on so I'm just going to get a random integer random integer oh between 0 and three because this says it's uh between 0 and Max minus one so three is going to be the max now the problem is here I can't actually set the um the variables of the base projectile this is because this node is of type actor class so if you right click here and say um uh we should be able to change the pin type yeah so if I just remove this and right click here and change pin type to be our base projectile class reference now we have access to all of our things here nice so I'm going to keep everything as a default maybe just make it a bit faster like yeah a thousand gravity zero no Target no homing everything's the same um we do want to add an owner though because if you recall well um owner is reference to self this is how we ignore Collision um let's let me actually remind you because this is going to be important so our projectile base if you recall here um we set ignore actor while moving uh to be our owner this is something added extra I added to either owner or instigator if instigator is valid it uses that if not uses owner but doesn't matter the main thing is that we ignore the owner from our Collision so we don't actually accidentally collide with the actor that spawned the projectile um it's not going to work exactly but you you'll see in a bit so now after spawning this one thing to remember also is that we have this event that we dispatched I actually called it on projectile impact so this on projectile impact was called right here when we collided um so back to our player we can bind to this event so we can say bind so I pull off of the actor that we just spawned the projectile and I say bind to on projectile impact now this will give us an event so I'm gonna just move these things a bit here so I'm going to create a custom event called damage enemy for example and we have the actor and the hits results so the hit results are just gonna put them here and the actor I'm going to plug it to our take damage here and plug this here um I don't need to spawn an emitter because this is already being handled by the projectile and this take damage is something that we covered in the damage system tutorial so I'm not gonna go over this one um all right so let's see if this works oh I got some errors oh I forgot to add a transform so this transform is where do we actually spawn this uh projectile from in my viewport I want it to spawn like maybe somewhere here around the stomach or chest so I'm going to click on my mesh and say add scene click on this last one scene and this is just a scene component where we can get the transform of it doesn't actually do anything in game I already have it so I'm not going to create another one but how we use the scene component is that in our event graph here we get this scene and get World transform and that's going to be the transform that we'll be using compile again we still have some errors because I didn't compile this one great now everything looks good so let's play and see what this looks like now if I click it should spawn a projectile and it's doing that but you'll notice that the projectile is also colliding with the player and I'm taking damage so why is it colliding with the player since we have this ignore actor while moving well if you hover here uh you'll notice that what the description says tells this component whether to ignore collision with all components of a specific actor um second line says components on the actor may also need to do to be told to do the same when they move that means that in our player we also need to ignore Collision as well so off of our spawned projectile here we're going to say ignore actor while moving choose our box Collision oh sorry I'm doing it the Ops Direction off of our capsule here we want to say ignore actor while moving and the actor we want to ignore is the projectile we just spawned tidy this up a bit great oh and should ignore is true now it shouldn't collide with the player anymore yes perfect and if I aim it at the enemy enemies taking damage and enemy dies awesome and this is how you would implement this system in an actual game um now I can show you how the other things will work as well um let's say here we want a Target um I'm gonna create another let's move this down a bit so on right Mouse button if right Mouse button is pressed then get actor of class enemy and promote that to variable this is going to be our Target I already did that before so I forgot that I already created this variable oh so I'm just going to set our Target here and once I release the right Mouse button set the target back to nothing and here it's going to use our targets that we just created so if the right Mouse button is pressed so we're going to be targeting the enemy otherwise we're not so let me test it out here right now I'm not pressing the right Mouse button so it's going wherever I'm looking at now I'm pressing it so it's going right to the enemy that's how targeting Works nice perfect thank you for watching I hope you enjoyed this tutorial if you did please consider giving a like And subscribe I'm going to try to do tutorials every week or two so if you have an idea or a specific tutorial that you want me to do please leave in the comments below thank you [Music] thank you
Info
Channel: Ali Elzoheiry
Views: 76,338
Rating: undefined out of 5
Keywords: Projectile game dev, Projectiles, Projectiles unreal engine, Unreal Engine, game dev, game development, physics, projectile motion, tutorial, ue5, ue5 beginner tutorial, ue5 tutorial, unreal engine 5, unreal engine 5 beginner, unreal engine 5 beginner tutorial, unreal engine 5 tutorial
Id: hkQ9bEwpfV8
Channel Id: undefined
Length: 44min 54sec (2694 seconds)
Published: Tue May 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.