How To Create A Homing Projectile - Unreal Engine Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another arrangement 4 tutorial in today's video i'm going to be going over a homing projectile so when you shoot a bullet it is going to be homing and it will lock onto different targets based on the closest one to the bullets now this is very easy to adapt onto so at the moment i've just got it so these boxes are the homing targets but the way i've set this code up is you can have anything you want can be a homemade target as long as you specify it when creating that object for example this can also work on players as well if that's what you wanted so let me hit play and show you what we're gonna make today so if i have to shoot towards this box here so let's shoot to the left of it you can see that the homing target and the projectile is going to go towards it as you saw it kind of went around and hits it from the back as that's just where the homing took it so if you do it here as well you see this would go straight past it but the homing went towards it and made sure that it didn't hit it instead again you can see it's just going to go for the closest one so that one it was going for the other and then it switched because there was a new closest one towards it so this is what we'll be setting up today the simple homing projectile which again is very very easy to build and advance upon yourself if needs be so this is nice and simple so without further ado let me do this code and i'll show you how i've done it so the first thing we want to do is we want to open up our projectile bp or the bullet which you are shooting now if you don't already have one set up what you can do is right click go to blueprint class and create an actor naming it whatever you like for example bullet bp and then open it up straight away but again i already have one which for me is first person vp blueprints first person projectile and again if you've only just made this what you'll need to do is add in a sphere collision and a sphere as well for your bullet and add in a projectile movement component as well i do have other videos going over a little bit more detail of creating different projectiles but i'm going to assume you've already got it set up so once we're in here what we want to do is we want to select the projectile movement component over here and we're just going to search for homing so as you can see it's nice and simple the main part of it is already set up for us we just need to tell it what to do so you can tick is homing projectile here by default but i'm not going to do it just yet and you can change the homing acceleration speed to be whatever you like as well i've set it a value of 3 000 obviously the higher the value the quicker it's going to go towards the target so set that to be what if you like i might actually increase it to 5 000. and again you can take this here if you want but i'm not going to the moment just because let's say it doesn't find a target i don't want it to try and lock onto something which it can't do so that's your personal preference if you want to tick it or not if this is only going to be a homing projectile then you may as well take it now we're going to compile save and then we're going to create a new function by pressing the plus function here over on the left i'm going to name this one find homing target like so and as it sounds this function is going to find the closest target for us to log on to so out of this we're going to get all actors of class with tag make sure you do have the with tag there and that is how we're going to specify which objects we want to actually be able to home and lock onto so again you might have one object next to another one and you only want one of the objects to be a target to lock onto this is how you do that so the actor class is just going to be actor and the tag is going to be homing make sure you spell it perfectly like so and what i'm going to do is ctrl a and ctrl c to copy that so we can use it later on making sure we spell it correctly and we're going to enter like so because again we're going to need to create this tag later on which is why i've just got a reference to it and so out of out actors what we want to do is we obviously want to access each individual target in the level so we're going to do a for each loop like so and we don't want the break because we do want to go through every single one in the level because what we're going to do now is we're going to find the closest one to the bullet so this is very simple i've done this in previous videos before so i'm going to go over again out of the array elements we're going to get the actor location and that's obviously going to get the location of the current homing target we're looking at and underneath this we're going to right click get actor location and this one is just for the bullet location as you can see the target here itself so this is the location of the bullet then we're going to come out the top get out location for the homing target and get a vector minus a vector with the bottom value being the location of the bullet and then we're simply going to get a vector length out of that so what this is doing is this is finding the distance between the homing target and the bullet so again that is now how we've got the distance between them let's now need to compare this distance to the current distance of the closest current target so what we're going to do is come out of the return value and get a float is less than a float because we only want to change the target if this new distance is closer than the previous one so we're going to right click and promote the variable on the bottom value of that naming this closest distance and we're going to compile and save that and we need to set the default value to a number which is really really high so again the first one we look at is always going to be closer so what i'm going to do is just press 1 hold down 0 press enter and that is going to be my number so i'm going to compile and save it doesn't need to be a specific number just make sure it's really really high so the first one we look at is always going to be closer than that that way we are setting a target for us to go to so then i'm going to hold down b left click to get a branch connecting the execution into the loop body and the condition into that less than float there because we want to actually be checking to see if this is closer than the current closest target in this loop body here so when we go into this it's going to get that current target location and get the distance between the bullet and that location and see if it's closer than the current target already and if it is we'll then want to set this to be the new closest distance because if it's closer this is now the new closest distance so we're just going to set closest distance off of true connecting that into the vector length there like so as again that is the distance between the current target and the bullet like so and what we also want to do is we now want to set this array element so this current target as the current homing target so we've set the distance we now also need to set it to be the current target that the bullet is going to lock onto so what we can do is simply right-click array element promote a variable and name this homing target or current homing target anything along those lines and again set that off of true of the branch that is what we need to know here so we compile and save that and as you can see what we're doing now is we're going to be getting all of the targets and which we can't lock onto seeing which one is the closest and then setting that to be the current homing target which we're going to lock on to and so now we're going to go back to the event graph in here and in some empty space we're going to right click and add a custom event naming this one go to target or fire towards target or lock on or anything along those lines so actually i might name this one lock on or anything like that again whatever makes most sense to you and out of this custom event we're going to call function find homing target which again is going to do all of this code we've just set up and actually this has also just reminded me we do need to do one more thing because we need to set the homing target but we don't have that as an output yet so what we're going to do is we just need to reopen the function select the input and then just simply add an output like so naming this one homing target like that and the return note of this is going to go off of the completed of the loop so once the loop has finished we know what the current target should be so we're going to exit this function out of this return node and the homing target doesn't want to be a boolean it wants to be a scene component and the reason we're doing that is because the bullet projectile homing target which is already set up for us in the engine has to use a scene component to lock onto so that's why we've done that there so now we're going to get our homing target reference which we created earlier which is over here and now what we can't do is we can't just connect that straight in there because this is an actor and this is a scene component so we're going to come out of homing target and we're just going to get component by class and the component class is going to be a scene component so it's just going to get the scene component of our homing target nice and simply return value going into the homing target like so now if we compile save exit this again you can now see we have the homing target coming out of the find homing target function perfectly like so so now we can continue on with this lock-on code so we're going to get the projectile movement components from up in the top left drag out of this and set homing target component which again is something already set up for us and the homing target component just wants to be our homing target from the function we just set up and again out of the projectile we're now going to set is homing projectile which is the boolean we were looking at at the start of the video and we're just going to take it to be true because we found a target so we now want to make sure that we're telling it that this is a homing projectile so we're going to compile save all we need to do now is make sure this lock-on code does fire off so it fires off the rest of the code as well so underneath this we're going to hold down p left-click to get event begin play but out of this it's just simply going to go into lock on like so or whatever you named this custom event we just made and what i'm also going to do is make sure that this is looping so it's going to be constantly updating on which is going to be the closest target so let's say it goes past the target too quickly and it's now closer to a different one it's going to lock onto that or if it's going after a player and another player intercepts it it will then lock onto that player so very simply all i'm going to do is hold down d locate to get delay connecting that into lock on and completed goes straight back into lock on as well so it's just going to loop constantly like so until the bullet is destroyed i.e it hits something and gets destroyed like so and the duration you can set whatever you like i'm going to set it to 0.5 so every half second it then tries to find a new target you can change this to one second 0.01 so it's doing it really really quickly it's really up to you we're gonna compile and save and that's all of the code we need to do but we do need to specify which targets we do want to actually be able to log on to so again if we open this function you can see it was get all that sort of class with tag with the tag of homing we now need to set that tag so we're going to close this and open up every blueprint which you want to be able to log on to which for me is justice homing target bp here but again set up for all of the ones which you want so this could be players or other actors which i've got here make sure you select homing target bp self up in the top left and then in the top right search in the details for tags add an array element and just add in the homing which we copied to our clipboard earlier so again name that whatever you have it named as before making sure it's spelt exactly the same we're going to close compile save and close all this and now we can hit place to test this out as this should be working perfectly for us so again if i were to shoot near this one you see it's going to lock on towards that and it did hit it perfectly like so so i think that'll be it for this video as we've done if you want to do we set up some code which now creates a homing projectile which is going to lock onto different targets which we have set again it's very easy to customize so you can see this kind of goes past them a little bit and that's just because of the speed so if i had to open this up select the projectile movement i can then lower the speed down to maybe 2000 it's not going to go past it as quickly as you can see there it locks on nice and easily like so which again works a lot better on long distance because it has the time to be able to just lock on perfectly and again it's kind of going a lot of different directions because i'm updating it to lock onto new targets when a new closest one appears and i have a lot of different targets in the level again as you see if i just shoot near this one it's going to lock on towards it and actually went for that one over there as you just saw it moved so thanks so much for watching i hope you enjoyed 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 in the next one [Music] you
Info
Channel: Matt Aspland
Views: 34,127
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, 3d modelling, blender, unity, games design, graphic designer, ue5, unreal engine 5, homing, homer, home, projectile, missile, rocket launcher, rocket, launcher, rpg, gta v, shoot, fire, ue4 homing projectile, scene component, update, change, targets, target, lock on, lock-on, lock, on, follow, gun, player, moving, object
Id: tNQjoyLxKkA
Channel Id: undefined
Length: 11min 59sec (719 seconds)
Published: Sun Sep 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.