Unity Tutorial: Building Placement

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to this unity c-sharp tutorial on creating a strategy game style building placement setup so basically I'll be showing you how to create a simple menu and when you click on them it instantiates an object which you can then drag around with the mouse and place it with a click and when you select these it will it will register that you've selected it and a menu will pop up so here obviously if you select your barracks you might want a couple of buttons to pop up with different units you can buy etc so yeah we're going to create a new project and walk you through how to create this so I just have a simple scene setup here with a tower a fort and a barracks model as well as a ground plane and a camera just looking down on them all now the first thing that we're going to do is we're going to take these three models here and we're going to create empty parent for each of them which is just going to contain some standard information which will be just a tag and a layer both of which you can just call building quickly assign those okay now we're also going to want to add a rigidbody component and we'll just change that to kinematic so the rigidbody component is just so that we can have access to the ontriggerenter and ontriggerexit those met those methods and we can also just add a box Collider here now I'm going to duplicate this and just move these away from each other and we're just going to drag each of these models into into there and let's open these up and just make sure that there all zero down just at the same position as their parents I did do that one okay and now we want to create their bounding boxes so we're starting with the barracks we'll just drag this out just a little bit past this and same thing for the fort and finally for the tower we're actually going to have a sphere Collider I'm just gonna replace that so just something like that should be good so it doesn't matter that it's not encapsulating the entire tower basically the idea is that on on this sexual like tower model here you could have your your actual like Collider if you don't want stuff to be able to walk into it or whatever this is just the sort of bounding box that we're going to be using to make sure that you can't put a tower inside of another building so it's basically just the radius that we're worried about you so um let me just name each of these properly so this is the fort and finally the barracks and we'll just see I've already got them set up from when I was testing but just drag them in and that there are not prefabs and we can just delete them from our hierarchy now um we're going to create a new script and we're just going to call this the building manager so this script is going to it's going to contain some GUI code so um it will it will show a list of buttons over here and when you press one of the buttons it will then send the the one that you've selected it will send it to another script which will be called building placement it's obviously the scripts will manage their actual movement and placement and attach to each object is going to be a last script called placeable building okay now um both of these the building manager in the building placement are going to be attached to the camera now if we just open up the building manager the reason why I quit and monodevelop just a moment ago was because it was some reason not showing my cursor so now let's just create the on GUI method and we're also going to make a public array of game objects and we can just call this buildings okay so in the on GUI we're going to loop through each of those we're going to say for int I equals note I'll s and buildings dot length and just increment I as well now in here we're going to say if you if you press the button which we're about to define with a rectangle as its bounds so for its width we can just say I mean uh for its width for its X we'll just say screen the width over 20 for its height we will have screen height over 15 and then we also want to make them appear one like each each one under each other so um we'll just add a screen door height divided by 12 and then multiply that by I so each time that it runs through this the button that it'll create will be below the the previous one and then for the actual width and height of the button we can just have maybe a hundred by thirty should be fine and then we'll just get the name of the building say the I fell iment name just like that should be good and we also going to want a reference to the building placement script so to say building placement and we can just call that building placement as well and now these are both attached to the camera so to instantiate this we can simply say building placement equals get component of type building placement except not the variable but script just like that okay now in here we are going to have a public method which we are going to call set item and this is going to take in a game object called B as its argument so this will be passed in when you press the button and for now just to make sure that it's working we'll just log B dot name okay so here we want to say building placement and set item as buildings the eighth element and that should be it so the last thing we need to do is go into unity and hold on we have a error just forgot an S on the end of building I should get rid of those now we want to assign everything to this array over here so if we go into our prefabs we can just assign them one by one actually just as a matter of interest I want to see I haven't checked yet can you add them no there's no way to add them as a group would appear that's a pity maybe they'll add that sometime in the future now let's test if this is working I've got barracks and you can see that it is printing out whichever one you press over here so the next thing that we need to do is actually to instantiate the prefab and be able to move it around so let's go back into who monodevelop or whatever other program you're using for your scripting and go into building placement and here we're going to replace this log with an instantiate but first let's create a private transform and we'll just call this current building and then we're going to instantiate this game object V as a transform so we'll just say current building equals instantiate B now I don't think that will work at the moment because instantiate returns an object I think and this is a transform so the way we'll do this is we will cast it as a game object and then just get its transform component just like that and that should work so in the update we're now going to say if the current building is not null just because there's no need to move something around that doesn't exist then over here we are going to put our code to move it to the mouse is position so let's just create a vector3 call it m and just set that to the mouse position now um the if if you're if you're using a orthographic camera then it's slightly simpler to do this but if you want to use perspective then there's just something that is just a line you have to add in manually basically which is just setting this m to a new vector3 and you've just got to get the m dot X and and y must be the same so that's obviously just across the screen and up and down the screen and then for your Z value you just want to get how far away the camera is from the from the ground plane which in this case is um you can see it's 15 so with orthographic camera it seems to do this automatically so you can just pass Mouse position straight into the method that we're going to do next but for here we have to do it manually we can just say transformed up position dot Y then we're going to create a new vector3 called P and we'll just set this to camera dot screen to world point and so this will just convert the value that we're passing in M to world coordinates and now we are going to set the the current buildings position just to a new vector3 and so we want that to be the X that we've got and wanted to be at zero height so it's positioned on the ground and P dot Z so now let us check if that was working go into unity so we can move the tower around yeah obviously can't place it yet so let's just go into orthographic mode and see if it's working as well see ya both of these Moses were are working so you can have a perspective or a orthogonal camera now we want to make it so that you can place it so we'll create a booth called has placed and basically we'll just say that when you press down the left Mouse so mouse button down zero then has placed equals true and we'll just say that you can only move it around if the current building isn't null and it hasn't been placed and then of course if if you add a new item then you want you want his place to equal false again so that you can move the new item around so let's test if this is working tower we click and it's placed yep it's all working good but unfortunately we do have a problem and that is that you can put things inside of other things which you probably don't want so we're now going to use the bounding boxes that we set up right at the beginning to make sure that that they are not going inside of each other so let's just create a method here called um call it is legal position and we can just make this a boo so that it's just for now just make it return true otherwise it's gonna moan at us for um not having a return statement now in here we'll say if is legal position and only then if it is a legal position can we place the object so obviously in here we are going to have all the code that will test if the colliders are well colliding now in the placeable building this script is going to be attached to each one of these let's just quickly do that now at a place for building so this place for building you might want to extend it with other classes called like tower and fort to add functionality to each of these buildings but for now basically all that this building is going to have is a list and to get a list we need to import the systems or collections dot generic so it's going to have a list basically of all the colliders that are currently colliding with this object so we'll make a public list and this is going to be of type collider collider and we can just go ahead and call this colliders and instantiate it as a new list okay now we don't want this to show up in the inspector so we can just use this command here hide an inspector so we can so access it from other scripts but yeah it won't show up in the inspector now um I'll have a void on trigger enter collider C so this is why we added rigid bodies and made the collide the bounding kaleidos triggers at the beginning so we want to say if seedot tag is equal to building so this is where the building tag comes in then colliders whoops there's an eye in there colliders dot add and I'll add in C and then in the ontriggerexit method it will be pretty much just the exact opposite on trigger exit instead of ad we want to remove it so basically as you move your tower around as it enters the collider that a dentist will be added into this list and as it exits it will be taken away so here in building placement we want to have a reference to the current buildings placeable building script such that we can access its colliders variable so we'll say private placeable building and we can just call it placeable building and then when you set the item we'll just set placeable building equal to the component placeable building of the objects just like that so now in here and we're going to say if the if baseball building dot colliders dot count which is basically the same as length for an array is greater than 0 then return false because then obviously it's colliding with something so we don't want to be able to place it now let's check if that is working caleda yes I'm not surprised that can't be found new coal Ida I don't know how I spelled that so wrong see I should get rid of the error now if we put a fourth down and a barracks you see it's not letting us place it inside there but as soon as we are outside it is still not letting us place it so um we have half the intended behavior working um we have just got to figure out why this isn't working and it isn't working because I spot Exeter's xed so now let us see if this is working forth we can place it there but can replace it here no we can't so that is absolutely perfect can't be placed inside any of these but as soon as we move it outside it works so the last thing that we want to implement is something like if you put a barracks down and now click on it a menu comes up with say like um units that you can buy from it or you know whatever whatever you might want in your game so um we're going to use Ray casting to detect what or rather which building has been hit so um over here we'll say elf so basically if you haven't got a building that you're currently moving around then if you can just copy this over here if you press the left-mouse down then we are going to do a ray cast so the things we need are a ray cast hit called hit and we'll just instantiate this and we also need a ray which you can just called ray those new ray and we need to give it a position and a direction to fire this ray in so we're going to want to use these um mouth positions that we set up earlier so um I'm just going to take it out of that if statement and just put it up here so that we can access them both from inside this if statement and from inside here and we'll say new vector3 and we will just get P dot X we'll pass an eight as our Y value that should be above your tallest buildings easily and P dot Z okay and then we also need a direction so for that we want to fire it straight down so we can just say vector three dot down okay and then finally we want to have the actual if statement which is if physics dot raycast basically if this ray hits something and we need to pass in the Ray that is going to fire and now we're going to say out and we're going to pass out the hit variable that we created I'm going to explain in more detail what this actually means in a moment and then finally we want to pass in a layer mask in a distance of the distance we can just fire it for infinity and for the layer mask will have a building's mask okay we're going to create a public layer mask over here called buildings mask so basically this is going to be just the building's layer that we set up right at the beginning so this if statement will only and true if the ray hits one of the buildings and then the test will say debug log hit dot Collider don't name now I just want to demonstrate what this out does where is it yeah over here so um say you have a string or a small F called T and say that is old okay if a string T equals old then we say set string and pass in T and then debug log T and if we make a method over here called search string that takes in a string T as an argument and then sets T to new you'll see that when we run this it's some it's printing out old even though we set it to a new over here which is just because string T here is a is a copy of this variable so what out does if you say out T and over here we say out string T then when we run this you can see it's actually been set so um basically it tells it not to make a copy of this to just set its original value directly okay now let's just test if this is working so if we place a fort and then click on it you can see it's printing out its name over there yep working okay so in placeable buildings we're going to want a new method called um set selected and into here we can just pass a blue s and we'll just say is elected equals s so we'll just make private pool over here called is selected and then um we can just have a void on GUI just to test that this some selection code is working and I'll say if is selected and then we can just um you can just make a button won't add any functionality to it but it's a rectangle that maybe a hundred by two hundred just like that and we'll just pass in the name just so we can see what is currently selected okay and then back in the building placement over here we want to say um hit dot collided game object dot get component and we want to get this some play scible building component dot set selected true okay so when you click on it it will set it is selected variable to true so let's just make sure that this is all working option now so if we put a tower down and a click on it prints out our barracks now you can see it's printing on top of each other because um when we're selecting this one it's not setting the others to false so we want to make a a private reference to this place ball buildings component but we want to call this place level building old okay now I'm in here we'll say placeable building old equals and then this component here ok so now we have a reference to the last object that we selected so now we we first want to say if you don't if you don't hit anything then just say placeable building old dot set selected false and also if we click on a we just have to make sure that old the the placeable building old isn't null we don't want to get a null reference exception somewhere along the line and then we want to do the same thing above here so this would be in the case that you've you select a building and then its menu pops up and then you select another building so it will set the old building to false and then the new building to true and then the old building will become the new building so I hope you follow that so we place a tower down and it barracks down and a fort down after click on fort fort if we click off it disappears if we click on this it turns into a tower which are in this barracks so you can see this is all working working quite nicely so I think that is everything for this tutorial I hope that has been helpful that you've learned something and yeah good luck with your game Cheers
Info
Channel: Sebastian Lague
Views: 113,667
Rating: undefined out of 5
Keywords: Unity (game Engine), unity3d, tutorial, raycast, c#, strategy game, GUI, video game, howto, help, tips, beginners, programming, yt:quality=high
Id: OuqThz4Zc9c
Channel Id: undefined
Length: 30min 15sec (1815 seconds)
Published: Sun Jan 27 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.