How to Spawn A.I into level | Unreal Engine | Racing Game Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome back to another video this video I'm going to be showing how to spawn AI into the scene as well as setting up some foundational code for the race position which we'll be doing in the next video so with that let's get into it so firstly we're going to start off in the sports Caron we're going to create some variables so in the variables click add and then the first varable we're going to add is distance point this is going to be of type float of type integer sorry nothing float integer and we're going to put it in its own category going to call the category position and then with distance Point selected hold controll D to duplicate and then we're going to add another one called position also of type integer and then we're going to add another variable called last distance this is going to be of type float and then the last variable we're going to call race length this is also going to be of type float and then that's everything for here going to compile save close the sports Caron and then next we're going to head over to the race spash widget so here we're going to make a couple of changes first we're going to add a vertical box add the vertical box just above the race progress and then add the race progress into the vertical box rename set vertical box to race info make sure to check is variable and then with that we're going to add a horizontal box into the newly created race info and then in the vertical box horizontal box my mistake we're going to add three text blocks and then the first text block you're going to hit it to fill change the text to a one change the size to 50 second text block we also going to remove that send it into a divide signal change it to fill and then change this also to 50 the text size and then the last one we're going to change the text to three hit fill change the text size to 75 so we're going to compile save make a couple changes resize the race info vertical box to fit everything make sure everything fits in hit the eras progress hit it to fill hit the hard horizontal box hit that to fill and then with that we going head over to the graph and then in the graph we are going to add the sports c as a variable so underneath the as vehicle player controller right click look for get player PA from the get player Pawn drag off and say cost to sports car connect the execution pin and then from the sport scn blue pin drag off promote to variable and that will be that and the adventic we are going to drag in the as vehicle player controller hold control drag it in drag off look for get race complete hold B left click to add a branch node connect it to the condition connect the execution pin then we're going to drag in the S sports car porn hold control drag off from the SOS Cort type get position you're looking for the get position variable and then drag in the go back to the designer make sure that the first text block we added to the horizontal box is head to is variable and we going to change the text [Music] to player position and then the third text box we're going to change this to Max cars also make sure it is variable compile save back to the graph so with that we now have the player position hold control drag it in from the player position drag off and say set text by the widget set text connect the execution pen from the branch note to false to the set text and then take the pin from the position connect it to the intext and then that is how we're going to set this up so we want this to con to continuously update that's how we adding it to the Avent and we're adding it to the false for the condition so that for as long as the race is not complete it will continue to update the text with that done we're going to compile save and then we're going to come back to this in a little bit but for now we're going to head over to the player controller and then we're going to change out the race progress for the race info so we're going to do it quickly wherever the Race Tech the race progress is remove it and then replace that with the get race info so with that we're going to compile save head over to the pp tracker so in the pp tracker we're going to be adding some extra component components in the vi Port under the components add we're going to add a sphere just the normal sphere we're going to rename the sphere to position one we're going to set it to static and then also change it Collision property so it says Collision preset says block all change that to no collision and then we're going to duplicate this control D angle position two compile head over to the level and as you can see now here with the uh BP tracker bullboard used to be there now sphere select it make sure it's BP tracker selected and then down the details panel going to come and select the sphere if you don't do that what's going to end up happening if you don't do the step you going to end up moving the entire tracker instead of just the Spheres so we're going to grab that and then just move it to somewhere on the road we're going to grab the second one and do the same thing just make sure that it's above the road and then we're going to remove the support the AI support SC do edit remove that save all head back to the BP tracker so now with that done we can now go to the event gr and here we are going to add a new function this function is going to be spawn AI so in here we going to set the code to spawn the AI at the sphere locations but first we need to add a couple variables so in the event in the event grow by the apply settings. sequence move this up and then from the set sports card drag off and say get all actors of CLA CLA we looking for is the BP AI BL out would exess drag off and say get a copy and then from the get a copy drag off and then promote to variable this variable will call spline we will be using this in the next video but we setting it up now so that we have less to do in the next one compile head back to the spawn AI function and then we're going to create another variable we're going to call this cars it's going to be an object reference to the sports Caron and it's going to be an array compile and Save and then hold control drag in the caruse variable from the cuse variable drag off and say add and then what we're going to add at the beginning is the sports car so hold control drag in the sports car variable connect that to the ad and then what we going to do is drag in the two positions these two spheres drag in both from position one drag off and say get World transform copy and paste then connect that to the second position and then from the ad drag off and then save four each Loop for each Loop drag off from the array say make array add a second pen connect the get World transform disconnect and then reconnect and then from the 4 each Loop drag off from the loop body there one one AI from class the class being the sports car p and child and then by the 4 each Loop element right click split Str pin drag from the location connect it to the location by this one AI from class from the return value drag off and say cost to sports called por and then we're going to drag in the C variable again hold control drag off look for add connect the execution pin and then connect the as sport car pin to the ad and then that will be all comp file and save so with this code you are going to spawn two of the AI that we created into the level so let's just go check that out see if it works and then if you still on the simulate click on the three buttons and then say selected viewport and the BP tracker in the Avent graph after the start sequence draging the spa AI function connect compile save head over to level save all play the level so now there are two AA that was spawned so the code works and then since the AI just drives off we're going to quickly correct that in the AI folder open up the sports car for child and in the on the event begin play we're going to create a new variable first and this want to say start and of type Boolean we going to drag off from the set bline going to add a delay by the switch add a delay going to set the delay to the same as the player 4.3 seconds and then we're going to hold alt drag in the start AI connect that and then set the variable to true and then by the event the event cck before everything else happens we're going to drag in the start AI get and then we're going to add a brunch Noe connect that in between so now the code will only start running after the 4.3 seconds have started so let's see if that works compile save save all play through the level the AI isn't going anywhere and then they drive off okay so that's not done and then we're going to head back to the BP tracker because you don't want to see these spheres when you play through the level click on these spheres and then we're going to look for the variable hidden in game we're going to check rendering hidden in game to True do the same for the other sphere compile and save save all check again see if it works okay this fear is no longer up here so that's that we got want to see if it actually did add the cars into the array so from the Cars variable array drag off and look for length from the ad drag off and then say print string connect the add to the inst string expand change the duration to 10 compile and save we just want to see if it added three vehicles to the array which would be the sports core players car and then the two spawned AI Vehicles save all play through the level okay so it's two and three so there are three vehicles in the array so with that we know that the array works so we can move this so with this part set up we're going to head back to the race spash widget and then we are going to create a new bind head back to designer choose the max cars the text bind create binding and then with this binding we're pretty much just going to do what we did now drag off from there get all AES of CLA the Clause being the BP tracker the out access we going to get copy drag off from get look for cars get cars from the get cars drag off and then look for length connect the length to the return value and then that should be everything just tid things up a bit so in this drag everything comment get all CS within array we're going to compile and save and then to see this works going to go back to the pp tracker compile and save over here and then we're going to disconnect the make array drag off from the array by the four each Loop say make array and then only connect one of the cars compile save save all play the level so it only says there's two CS in the array head back to the pp tracker add an extra pin connect the bottom one compile and save save all play the level now it says this three cars in the array so the code works so that's it for this video we're going to continue with the race positions in the next video so with that until the next video
Info
Channel: GenuImitation
Views: 59
Rating: undefined out of 5
Keywords: #racing game, #ue5, #GenuImatation, #unreal engine, #tutorial, #gamedevelopment, #unrealengine5
Id: 31oCaxZmvGs
Channel Id: undefined
Length: 15min 53sec (953 seconds)
Published: Wed Apr 24 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.