Unreal Engine 5 Tutorial - Spawning Actors or NPCs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] thank you what's going on everybody James here from artificial entertainment and welcome to another Unreal Engine 5 tutorial and today we're going to be looking at the principles of spawning so let's go ahead and dive in so when it comes to spawning there's two main things that you need you need a location and as well as an item to spawn at that location now there are also other things as well such as rotation and the number of items that you're going to spawn or the number of instances of an item that you want to spawn But ultimately so long as you have an item to spawn and a location to spawn it at those are the two main things that you need now I'm going to be using an AI character as the spawnable object today but if you're trying to use this process to be able to spawn actual objects in your game like weapons or potions or other things you're going to use the same process you're just going to switch out one node which I will go over during the demonstration so what we'll do is we'll go ahead and create our first spawner now for me I like creating spawners as actor blueprints so we're going to right click go to blueprint class and then click on actor we'll just name this spawn one and we'll double click on open this up now one thing I like to do with spawners is I like to add an arrow to these and this allows me to give you know some sort of visual reference so under the components tab you can just go to add type in Arrow and then just add one like this this way once you add your actual spawner into your game you have a nice visual reference for where it is and you can even color code these based off of if you have multiple different types of spawners now we're going to go ahead and pull up the event graph here and we're going to start coding in our first spawn and we're going to do all of this off of event begin play now you can do this any way that you want if you want to use custom events and have it called in from your game mode or however you want to do it but today we're just going to use event begin play as what spawns our actual AI into the world now for spawning this is where as I had said earlier there's two different nodes there's a first one that you would use to be able to spawn an actual object into the world and there's another one that you would use to spawn an AI that has a behavior system now if you're just trying to spawn an item you'd pull off of event beginplay and then just type in spawn actor from from class and you'll see this one right here under game and this will give you this where you can select any class that you want to spawn as well as the transform that you can split to get the location rotation and scale however today we are using an AI character so we're going to delete this out and we're going to instead spawn AI from class now this is the only difference everything else that you're going to do is going to be the same however if you're spawning in AI you want to use this one if you're spotting a regular object into the game you want to use the other node now as you can see here we have a pawn class just like the other one but we also have a behavior tree asset that we can choose as well so I'm going to go to the pawn class here and I'm going to choose my demo AI character and for his behavior tree I'm just going to select his main Behavior tree now one thing that's really important to keep in mind when spawning is the location value and specifically the Z value this is something I actually did incorrectly for a long time without even realizing it and what I mean by this so if we want to have the AI spawned at just the location of where the spawn is you would think you would just get actor location right and with the combined struct pin you could just do this right simple easy no problem well actually there is a problem so if I go and I put my spawner into the world here you'll see his Z height location or its Z High location is zero however if I go to my Ai and pull out this blueprint and put him into the world you're going to see his Z height is actually 88 which means that if I spawn him in at zero it's actually gonna be a chance that he'll just fall through the floor it actually won't spawn in correctly so what we want to do is make sure that we set this Z value to 88 and then just give it the location for the X and Y for whatever our spawner's location or get actor location is so we can just right click to split this struct pin we can also right click to split the get actors truck pin here and we're just going to hook up the X and the Y but the Z location we're going to set to 88 so this way it's going to spawn it at a proper height now from here we can just compile and save will minimize this really quickly and also delete the AI that I put into the world and if I go ahead and I just hit play you'll see that we have one AI spawned into the world so it has done exactly what we asked it to do we said spawn one Ai and it's bonded at the location right here where our spotter is and as you can see that Arrow does make a really nice visual for whenever looking for that spawner so let's talk about a little bit if you want to randomize that location make it so that way you know it doesn't just spawn in one location you want to spread the spawn out a little bit make it so you can spawn in multiple points just randomized well it's an easy way to do this as well if we unhook the X and the Y here and then recombine the struct PIN for get actor location we're going to leave this direct pin um split on the actor of class spawn but we're going to right click and get random if you want to get random point in navigatable radius but the return value of get act or location into the origin and just set the radius to something high like 1200 and now this is where we're going to go random location split the struct pin and we're gonna hook up the X and the Y into here leaving that Z location again just kind of at 88 because that's our proper Z height so now if I compile and save and just kind of bring this down a little bit and then hit play you'll see he actually spawned over here even though our spotters over here he spawned over here and I can hit play again do the same thing he spawned way over there so now he's picking a random Point within 1200 units of our get actor location and then just choosing a new X and Y value so this way it gets a random location on our actual floor so now let's talk about random rotation what if you wanted to give a randomized rotation as well what would you do for this well actually very easy there's actually a node in Unreal Engine for this so all we have to do is just right click and go random Rotator you're going to see right here random Rotator and we can right click on the return value and split this because we only want the yaw because the yaw is technically the facing rotation right so that's all we want to do is give them a different facing rotation so we want to split the strut pin on the spawn as well and then just hook up the yaw leaving the roll in the pitch at zero now if we compile and save and I'll move my player's spawn inwards because it's going to be hard to see with him kind of actually moving around but you'll see he just spawned kind of facing more towards the middle here and if I hit play again this time he spawned facing that wall you know this time he spawned facing that way so each time he's getting a different facing Direction and a different spawn location so now that we have this what if we wanted to do this but we wanted to spawn like five or six different AI into the game well again very easy one note Edition Alpha event began played you can actually just pull off of the execution pin here and we're gonna do for Loop now we don't want the for each Loop or the for loop with break we just want the for Loop standard for Loop right like here just like this now we're going to set the first index to one and the last index we're gonna set to five now what this will do is this will actually spawn in six AI technically speaking um but even though you're setting the last index of five it's gonna spawn in six so that's just something to think about but if you don't set the first index to one it'll spawn less than so if you set it to one it'll spawn at least one grader or it could be one less if you set it to zero for some reason no matter what you do the first and last index are never actually going to be accurate there's going to be some sort of like calculation you're going to have to put into this I don't know why but this is the only way that I've found that I actually makes some sort of sense and doesn't come back is kind of buggy but if I go ahead and minimize this now and if I just hit play you'll see that we now have one two three let's see where's the rest of them ah four five over there so this time it did spawn in five actually it looks like sometimes it does spawn in six um I just actually recording this yesterday and it spawned in six doing the exact same system so this is where it it gets a little weird sometimes but this is how you're going to be able to spawn in multiple um I'm working on a better system to make it so that way you can actually get the exact number constantly with control but there's still some bugs within the engine unfortunately now the last thing that I want to talk about is just basically if you wanted to give something like a specific location or rotation how would you go about that well we'll take out the for Loop here and we'll take out the get random point and off event begin play we'll just pull off we'll do a simple get actor of class now what you can do from here is just return value get actor location and split this dark pin and then do the X and Y of this actor location now something to bear in mind though is that this would only be necessary if you're trying to spawn like a merchant at a store and you're having the spawn point so the actor location if we go to the viewport here just like the third person character or actually we'll go to the spawn point the actual what it references as the actor location is this icon here so wherever this is in the world that's what it's going to be referencing as the location I can move this Arrow way over here just like this but it'll still reference this as the actual actor location it doesn't matter what I do that is the actor location so that's something to keep in mind so whenever you place like your shops and stuff if you place your shop with that at that location in mind for where you want your AI to spawn then you can use this code very easily well that's really it for today's video guys you know that's really it uh spawning is just all about getting a location maybe setting a rotation if you want in that location and rotation can be specified or randomized depending on what you need and you can spawn as many as you want you can even create location structs and a whole bunch of crazy stuff to be able to make your spawning truly Dynamic um it's it's really great what you can do now I'm going to be doing a video here soon about you know enums instructs and data tables all that good stuff there's some really really cool stuff that I've been playing around with so I really want to show you guys that um but also before that there should also be a video about the mapping variable type which is also really really cool so if you guys haven't checked that out please go and check that out mapping variable types are awesome for just kind of storing information that's all for another video so I'm gonna close out today guys I hope you enjoyed and as always stay animated
Info
Channel: Artofficial Entertainment
Views: 11,940
Rating: undefined out of 5
Keywords: Unreal Engine 5, Unreal Engine 5 Tutorial, UE5 Tutorial, Unreal Engine Game Dev, UE5, Unreal Engine, UE, Game Dev Tutorial, How to make games, Making Games, Spawning, Spawning AI, Putting, NPC, In world, UE5 spawn, dynamic spawning, random spawn, NPC random, spawning
Id: cRk1Wi7CfAU
Channel Id: undefined
Length: 10min 36sec (636 seconds)
Published: Sun Mar 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.