How To Create A Throwing Grenade Predicted Path Trajectory - Unreal Engine 4/5 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another unwrenching five tutorial in today's video what we're gonna be going over is a grenade projectile trajectory type thing so if you're throwing an object you're gonna have to see on screen where it's actually going to go in the trajectory in which it will follow so let me hit play and show you what we can actually make to explain it a little bit better so if i were to hold right click you can see we have this kind of line in front of the player and if i were to let go we're going to throw a grenade it looks a bit like an egg but you know it's a grenade and at the end it's also going to blow up but you saw it followed that exact path like so and i can increase the velocity or decrease velocity of me throwing it and that will also increase and decrease where the trajectory actually goes as this does work perfectly for how it's going to be now obviously the line itself doesn't look absolutely amazing that's just because of the meshes i'm using it's literally just a cube you can obviously spend a lot more time getting this to look a lot nicer for you same with the animations i've now got an animation just because i couldn't get it to look perfect so today's video is going to be kind of the very basics of this just the base fundamentals of setting up and you can obviously then adapt this to your own code to make it work a lot better and look a lot nicer the reason why i've got it as i have today again in this very basic form is purely just because this is the kind of the best i could go for for free or at least to make it easily accessible to a lot of people which as you know is what i like to be able to do but if you do want some additional help in setting this up further you can always let me know and dm me on discord with a link in the description down below to join my server but anyway this is what i'm gonna be going over and creating today so without further ado let me delete this code and i'll show you how i've done it but before we get into the rest of it here's a quick word from today's video sponsor so today's video is sponsored by stream stream is the number one chat api for games so what is chat api you might be asking it is what allows you to talk to other people in multiplayer games by text and or voice so as you can imagine implementing a chat api into a game can be pretty difficult and time consuming and oftentimes requiring a lot of work so stream provides scalable api solutions which means you can easily integrate real-time chat systems into your game so stream is completely free for all indie devs and startups so go check out and sign up for their free maker account by clicking the link in the description down below so thank you once again to stream for sponsoring this video and let's get right back into the video you were just watching so the first thing we're going to go over is looking at the animations which i have so what i did was i got this toss grenade animation from mixmo and it to the player character as you can see here now again this obviously isn't perfect as they're holding a gun but it's what i've got then i tried to split this up into two animations one for aiming the grenades although actually that's the edited one i did so one for throwing a green as well so what i did was i just split it in half so one for let me actually just show you on here first so one for just this first part of aiming it and another one for throwing it like so again it didn't work out too perfectly as they have changed animations quite a bit but i will show you where you can put your animations if you have your own proper ones because obviously this also isn't the best method anyway but again there's not really any free animations anywhere so once you've got your animation sorted what we want to do is actually set up our grenade or basically our storable object now you might already have this if so great you can skip ahead later on the video so what we're going to do is right click go to blueprint class and create an actor and i'm going to name this one grenade bp that's that makes more sense to me and we're going to open it up straight away in here i'm just going to do some very simple stuff i'm going to add a sphere collision like so dragging that onto default scene route to make it the new route and i'm also going to change its collision to be block all so collision presets to block wall like so then i'm going to add another sphere inside of here just to give my mesh of my grenade and let me just scale this down so i'm going to get to 0.1 on the x and y and then 0.2 on the zed just to give it a bit more of an oblong shape like so again looks like an egg but you get the idea and then you can obviously change the size of your sphere collision as well if you wanted but for me this is going to be all right then i'm going to go over to the event graph delete act to begin overlap and event tick and we just you want to use event begin play now in here i'm just going to do my very basic grenade code so what i'm going to do is hold down d left click to get delay setting that to 5 seconds so 5 seconds after the grenade is spawned in it's going to blow up to blow up i'm going to do spawn emitter at location with the emitter template being the explosion which you get in the start content and the location being get actor location so basically wherever the grenade is after this we're going to do play sound at location with the sound being the explosion queue which you also get with the start content and location once again being get actor location then i'm going to drag in a reference to my sphere 1 ie my grenade mesh and i'm going to set hidden in game just to hide this from the player so it looks like it's blown up the player can no longer see it and then i'll get another delay of three seconds just so we have time for the explosion to actually end and then we will destroy actors so we're actually going to be getting rid of the grenade in the level as well so we have something which looks like this now sorry if i went through that quite quickly i'm the only reason i'm speeding through this is just because this isn't the purpose of the video it's not to create a grenade is to create the trajectory for the grenade and again i imagine most of you probably already have all this stuff set up and if you don't this is it here anyway so that is all we need to do in here so we're going to compile save and close that next up we can actually start setting up throwing this grenade so what we're going to do is open up our content browser with control space then we want to go to third person blueprints and bp third person character or whatever you've named it we just want to open up the character blueprint and we'll compile save this and go straight over to the viewport here what we're going to do is add a component with this one being a child actor like so and i'm just going to name this one grenade reference if i could spell that correctly there we go and then what we're going to do is set the child actor class to be our grenade bp or again whatever it is what you're doing and i'm going to move this into the position which i want so now you might be wondering what this is for this is where we're going to spawn in so where we're throwing it from so if you wanted you could go into the minute detail of actually attaching it to the player's hand and making it follow that but what i'm going to do is just have it spawn in front of the player like so as i don't really think it's going to be too noticeable for me in my project so i'm going to tab it spawn in let's say here and we will compile and save that like so once we've done that we can just press the x next to child actor class to remove it as we don't actually need the grenade in there we just need the reference to its location so we'll compile save and go over to the event graph now now this is where the fun begins of actually throwing it so what we're going to do is right click and get right mouse button or whatever the button is for you which you want to be able to throw it from i'm doing right mouse button so i can hold it to be able to aim and you can obviously set up action mappings as well if you wanted but i'm not gonna be going over that right now out of pressed what we can do is play anim montage and this is where you then input your animation for actually throwing it so this is where you actually input your animation for throwing but again i don't have any i'm just showing you where you can put it if you did and then we do the same on release as well so we can also play one for aiming and play one for throwing after this we want to hit the plus variable and call this one is aiming question mark leaving that as a boolean so it's a true or false value so we know if the player is or isn't aiming i'm going to hold down left alt and drag and drop this in to be able to set it and i'm going to set that off of both pressed and released pressed being true release being false so we are holding down right mouse button ie is pressed we're going to be aiming and when we release ie we're no longer holding mouse button we let go of it we're no longer aiming so i hope that will make sense and like i say we're going to set up throwing first so that will be off of released let me actually input my thorough grenade montage in here like so so to be able to actually throw it what we're going to do is spawn actor from class so we can actually spawn in the grenade that class obviously being our grenade bp now for the spawn transform i.e where it's going to spawn we want to right click this and split the structure pin and the location like i said wants to be the grenade reference we just created so we're going to get the grenade reference drag out of it and get world location like so connecting that into spawn transform location then for the rotation what we want to do is we want that to be the player's rotation so it's always going to be spawning in front of the player so that's very easily right click get actor rotation connect down there like so and the scale i'm just going to leave as one one one you can obviously change that if you wanted but i've already set up the scale inside of the grenade blueprint itself so that is now going to spawn the grenade in but like i say we want to throw it not just have it up here so out of the return value what we're going to do is get sphere if we scroll all the way down we can get sphere like so and we just want the sphere collision so if you've given a different name get the sphere collision out of this we want to set physics linear velocity and this is what's going to actually give it that velocity so we're throwing it and we'll connect the execution into the spawn actor like so now the new velocity we need to do a little bit of math to figure out where we want to be throwing it and how fast because obviously velocity is both speed and direction so it's not too complicated what we want to do is right click and get actor location so we're going to be getting the player's location out of that we want to get the unit direction vector so we're getting the unit direction from the player to and we want to have this to the grenade so we're going to come out the return value of the spawn actor and get actor location connecting that into two like so so again what we're doing is getting the direction from the player to the grenade so we know which direction we're throwing it in and so that's the direction and then for the speed we just want to multiply this by a float so we're going to come out this and multiply like so so now we have a vector multiplied by a vector so we can right click on the bottom pin convert pin and convert it to a float single precision and then i'm going to right click promote the variable naming this speed so again this is direction this is speed and the return value of this multiplication is going to go into the new velocity of our set physics linear velocity like so so i hope that all makes sense again we just basically gain a direction adding the speed onto it and that's the velocity for throwing our grenade now the speed down here i'm going to compile and i'm going to set this to a value of 800 by default will compile and save that and obviously this is how you can just change it here so you just change this variable in order to be able to actually change how fast you're throwing your grenade now if i have to go down here you can see what i have is this little bit of maths down here so all i'm doing is when i press 1 i'm adding 100 to my speed and when i press 2 i'm taking 100 off of my speed now the reason i've done it like this is because i wouldn't actually have this in a game i just have this here for dev testing purposes and to showcase it off showing that it does work for the purpose of the tutorial but this is how i'm changing my speed and so let's go back up here and now this is throwing the grenade perfectly done and set up for it and it will also blow up as well so let's actually hit play and prove this so if we hold down right mouse button and let go we're going to throw the grenade like so however that actually didn't work and that's because i forgot one crucial thing and that's we need to set simulate physics so let's open it back up opening our grenades sorry that is great bp here and then if we just select sphere collision and then just tick simulate physics a very important thing again i just forgot it so now let's test this out hold right mouse button and let go it's going to be thrown like so and then it should blow up after five seconds as well as you can see perfectly there so now we've got it throwing let's set up actually seeing where it's going to go with the trajectory and this is actually probably a lot more simple than you think it is thanks to one very useful node so let's drag out of set is aiming true off of pressed up here and what we're going to do is simply get predict projectile path advanced again very very simple it's going to do it for us pretty much all we need to do is just input some parameters so we have the predict params here let's right click that and split the structure pin to open up and you see we have all of this here now this might start to look a little bit more complicated but trust me it isn't so we've already done a lot of this work already so we've got the start location well we already know what that is we can get the grenade reference and then simply get world location that is where the start is going to be because that's where we're spawning in to throw it from so that's nice and simple then the launch velocity again we've just done the maths down here to figure that out so all we need to do is select all this hit ctrl c to copy it move over here and hit ctrl v to paste it connecting that into the velocity there now you might be wondering why i'm not just connecting this one up that's because this is going to be firing off before we spawn in the grenade so this isn't going to actually return anything yet as there's nothing for it to return from so what we're going to do instead is just use this grenade reference and world location instead looking like this so we can move this over and now we've got something which looks a little bit like this and this is going to work perfectly for us so i'm going to move that down just to organize this a little bit more like so and that is the main part of it done that's now going to see where the path is going to go so what you can do as well is change the max sim time so this means it will only predict the projectile path for one second we want it to be a little bit longer than this obviously if you're throwing it further you'd want it to be going on for longer i'm gonna set it to three seconds as that works perfectly for me however again if you're throwing really far you want to increase this a lot more and the other thing i'm going to change is the predict params sim frequency i'm going to change that from 20 all the way to 100 so it's going to make a nice smooth trajectory instead of having it break up every now and then and just to test to see what this is going to look like i'm going to change the draw debug type to be persistent and let's hit play to test this out again so if i hold down right mouse button you'll see we have a nice trajectory line like so if i let go you'll see it perfectly followed it like so and again like this it's perfectly following it like that now you might quite like the look of this line here for this trajectory i really like it as well however as it's just a load of sphere traces we can't actually have this shown to the player now i believe there is a better way of turning this line trace into an actual mesh or into a path and if you do know how to do that do let me know in the comments down below as i believe there is a good way of doing it but i just couldn't figure it out today but again let me know so what we're going to do is go back in here and actually do this other way of turning it into a path which still works but obviously doesn't give as good results of how it could look so what we're going to do is right click predict result and split the structure pin then now we have access to this predict result path data array we're going to come out of this and get a for each loop we don't need the break just a loop and connecting that up into there like so then we're going to right click the array element and split the structure pin and now we have access to the array element location and so this location here is just the location of the trajectory so each individual point in the trajectory so what we're going to do is just spawn in a cube at that point again not the best way of doing it however it does still work very well so what we need to do is actually create that cube so that's very simple we're going to go back into our content browser right click create a blueprint class creating an actor and i'm going to name this one trajectory line bp opening it up straight away all i'm going to do in here is add a cube scaling it all the way down to be 0.1 and what i might do is just add 0.2 on the y as well just to make it a little bit bigger and then i'm just going to change the material to be red just so it looks a little bit different like so also very important make sure you do disable collision on this so if we search for collision what we can do is go to the cube itself sorry and just put it as no collision we'll compile save that and then the final thing in here go to the event graph and then off of event begin play get a delay setting this to be 0.1 seconds and then we're going to destroy actor after that so pretty much as soon as we spawn this in it's going to be destroyed and that is for two main reasons one so when the player moves obviously the trajectory line is going to update so we don't want this to still be in the air and also we're going to be looping spawning these because again we want to be constantly updating where the trajectory is so we don't want a load of these in a level lagging out the game so that is why we're doing this like so i will compile and save that now back to our third person character out the loop body we can get a spawn actor from class with the class being that trajectory cube we just created the spawn transform we're going to right click split the structure pin have the location be the array element location like so and the rotation scale and everything else i'm going to leave as default and then very simply to loop this all we need to do is come out completed get a delay with a duration of 0.01 completed going into a branch with the condition being our is aiming variable we created earlier because again we only want to be looping this and getting a predicted projectile path if the player is actually aiming so false will do nothing because it will then end the loop and true it's going to go back into the predict projectile path advanced like so and i'm going to double click this line to get some root nodes to keep it looking nice and organized and that there is the code done for us again there will be better ways of doing this however this does work exceptionally well for a free asset which is very easy for people to understand so again it's a good base fundamental code for it so we'll compile and save that close it all and we'll hit play to test this out i can right click and you'll see we have this here now the reason why that's doing that is i forgot to actually turn off the debug on the line trace so let me do that very quickly open up our character blueprint again and just turn that to be none on the true debug type like so now let's try this again and you'll notice it's actually despawning the cubes a little bit too quickly so let's have a look at why it's doing that and that's because i set this to the wrong number i think i said the right number but i put the wrong number in and this is in our trajectory line bp sorry i set the duration to be 0.01 when i meant to set it to 0.1 don't know if i said that wrong but i definitely put it in wrong so sorry about that but we need 0.1 in this delay before destroying it and let's just make sure this is right yep in a third-person character it should be 0.01 so you can obviously change these values a bit if you want but anything more than this i find makes it look a little bit stuttery but if you do want to change them make sure the third person character is spawning them in quicker then the trajectory line itself is deleting them so let's try this one final time and there we go now we can see it looks perfect and if we move it's obviously going to move with us as again they're being destroyed immediately and if i have to let go you'll see it's going to follow that path perfectly and if i press 1 and 2 to increase and decrease the velocity you'll see it's also increasing and decreasing where the projectile was going to go on this predicted path and you'll notice again it still works perfectly for us so i think that'll be it for this video to have done everything we want to do what we've done is we set up a trajectory system for our projectile so we can predict where it's going to go and it works perfectly so player can actually aim their grenade properly like this so thanks so much for watching this video i hope you enjoyed it and i hope you found it helpful and if you did make sure to like subscribe down below so thanks so much for watching and i'll see you in the next one [Music]
Info
Channel: Matt Aspland
Views: 35,997
Rating: undefined out of 5
Keywords: ue4, unreal engine 4, unreal engine, tutorial, ue4 tutorial, unreal engine 4 tutorial, how to make a game, how to, blender, unity, games design, ue5, unreal engine 5, unreal engine 5 tutorial, ue5 tutorial, grenade, path, trajectory, predicted, throw, throwing, predicted path, show, visualise, mesh, line trace, spline, ue5 predict projectile path, ue4 predict projectile path, turn line trace into mesh, convert, throwing trajectory, grenade path, throwing path, pubg
Id: TAnzmz2AviY
Channel Id: undefined
Length: 21min 0sec (1260 seconds)
Published: Thu Jul 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.