House Building System like Rust/Valheim! (First/Third Person | Unity Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome i'm your code monkey in this video we're going to do something really interesting so a while ago i made a really awesome house building system and then unity made the awesome free standard assets which includes a first person and a third person controller so now here let's put them both together to make the house building system work in third and first person i want to have a character in the world look around me and play some objects this is similar to games like valheim in third person or minecraft and rust in first person after watching this you can download the project files and use them to build up your games okay so let's do it easily build games with this video's sponsor core core is a free game creation platform that lets you build and publish your games it is great for very quickly and very easily prototyping any ideas you have and it is especially easy to build multiplayer games since all of the complicated networking parts are all handled automatically you have thousands of free high quality sound and art assets to build your game you can build entire games without code or if you want to take it further you can write your own custom game logic using lua once you have your game built you just click a button and it's instantly available for anyone to play also right now there's two awesome events running first the core traffic jam create a vehicle game using course tools make some cars boats or go crazy and build something truly original this one has a twenty five thousand dollar prize poll and starts right today and there's also the core invitational game jam with a massive prize pool of a hundred and forty thousand dollars including a brand new tesla model 3 applications close in just a few days so click the link right now to download core for free and build something awesome now like i said here we're going to combine things that i covered in two videos the house building system and the third person controller so over here i've got both working really the only tricky thing to combine these is handling how the mouse points to a place in the world since we're no longer on a top-down view but we'll get to that problem in a bit first let's look at how the house building system is working so here is the same thing from that video there's this overhead camera then i can click on these buttons to choose what object type to place and then i can simply move my mouse and click to place them so place down a bunch of floors and then place down a whole bunch of walls and finally a nice door and there you go there's my now's house so that's it this is an awesome system for building houses or really any structure now if you want to know how this system works you watch that video in the home grid system playlist and like i said after watching this video you can download the project file to inspect all the source code for yourself now the first thing we want is to add the third person controller onto this scene so for that i'm going to do it just like i showed on the star assets video so just scroll down over here i've got the star assets and the third person controller so first let's drag the main camera then let me also disable the camera on the house building system so over here my previous main camera as well as the virtual camera okay disable both those so i only got this one all right now add the player follow camera and finally the player armature so here it is yep there's the character and to set it up all i have to do is go into the player phone camera and assign the player root and just like this if i hit play and yep just like that everything is working so i've got my character and i can move it around now this character itself doesn't look very good in this setting so i'm going to use a character from the same asset pack that i used on the house building system there's a link in the description if you want to get it so i'm going to swap out the character just like i covered on the star assets video so just go into the player unpack it then i need to find the character so i just drag the character i get rid of the other two i remove the animator and i attach the avatar here and yep here it is everything working okay great so we have the house building system and the third person character both working in this scene all right so far so good now let's see the code for how the house building system is placing objects it's over here on this function it simply tests for the input get mouse button down so on the left mouse button then it used the mouse 3d class to get the mouse warm position so i cover this in a dedicated video if you want to learn more but really all it does is a raycast right directly from the camera screen point array so in the third person controller the mouse is always stuck in the center of the screen so this exact same logic is going to work without any issues for that let's just quickly add a crosshair so we know where the mouse cursor is so let me go inside the canvas and just create just a simple image for the crosshair and really just select the circle something very tiny and add a nice outline and let's also test to make sure that the mouse over position is correct so let's make a debug visual just to quickly see so just a simple sphere then on the mouse 3d script right here let's add another feel for our debug sphere and really enough they just put this one there alright so here i am and yep it does work so it correctly identifies the mouse position so the mouse is centered on the screen so everything is already working perfectly okay so far so good now let's test spawning a4 object for that naturally we need to change the input since the mouse is locked so i can't really go down into those ui buttons so over here is the ui script that handles all those buttons and all the buttons we only do is just trigger this function so let's trigger this using key inputs instead of the mouse so let's make a very simple private void update and on update simply test get key down and let's go with the alpha alpha one through something and just trigger that all right that's it let's test so here i am and as i press keys yep there you go with the enable and i can see the ghost so i can see that one that one that one and that one all right and now if i click there's actually a minor issue the issue is that the click tests if there is nothing blocking it on the ui but right now there's a crosshair right on top so that's an easy fix just go up here into the ui find that image the crosshair and just disable the raycast target that way it won't go through and it will not block anything so now here i am press the button click and there you go now i'm placing down objects just like that and yeah there you go here is a really nice house and now the door already works but actually the standard assets need to change one thing so over here on the player there is a script to actually allow it to push things so just enable this and now if i go and yep there you go i can now push the door and go inside my really nice house all right awesome so just like that we already have the basics working now the one big difference compared to the house building system is how do we handle multiple levels so when i made that system in there i went to the simplest approach of pressing a button to change the active level so right now i'm placing on ground level and as i press a button now it's up there on level one now and level two and so on but here since we are living in the world itself it makes a lot more sense to have that process automated based on where i'm pointing the mouse so how we're going to handle this is based on the mouse we're on position so if the player is looking at the floor the mouse position won't be on zero so it won't place it on level zero but if the player is looking a bit higher then the mouse by position will be higher so we need to do some math to identify the level okay so let's do that by the way if you find the video helpful please hit the like button it's a tiny thing but it really does help thanks so over here we're gearing the mouse one position and then down here we're testing for the get key down and then we're selecting the grid so let's automate this system so let's name this one the handle grid select manual and then have another one for the panel grid select automatic so here instead of the minimum function let's test using the handle grid selection automatic okay now here first let's get the mouse on position and now let's see the height of the walls so here's the house building system and the prefabs and let's go into objects and look up the wall object now just look at this and see just how tall this one is so here it is and as i move it up okay it's about 2.5 units okay so up here let's just take the mouse position dot y and divide it by 2.5 units so this is the grid height and actually this one should also be defined up here so let's see yep over here for the grid vertical size i'm already defining this so let's use this as a constant so we use it in the same place so we're in height then over here we use this one and then down here on the other function use the same value okay so now we can just do a math.round to round this value and then this will be the grid index so we go into the selected grid and we just select the one on this index so just with this very simple logic everything should already be working so let's test okay so here i am aiming at the floor and place it on the ground level all right now let's just place on a wall and as i move up yep there you go it automatically goes so as i go towards the midpoint of the wall it changes from the grid down there to the grid up there all right so just like this already working great so i can place it in there and there you go now i've got one down here and one up there and with this setup is made by using a raycast so it really works on anything that has a call either meaning over there i've got a nice mountain so as i go towards this mountain as i hit it yep there you go it automatically goes up there so yep look at that really nice so with that yep we already have multiple levels working now there's a few issues that we need to take care of the first issue is the distance so right now there's no limit so i can place an object all the way over there very far away that's obviously not intentional so let's add a simple check to that here when we're handling normal object placement we get the mouse position and now let's do a simple distance check from the player so here i need to add a reference for the player itself okay and then down here you get the most position then just do a vector three dot distance testing the player position and the mouse position and the final float for the max build distance let's put it at something and just test and we only do this if it's under that distance okay very simple let's test so here if i point on the ground and click if there goes place now i point very far away and i click and nope it did not spawn anything alright great now for the other issue which is the mouse position is based on collision so in the house building system that was an issue because the camera was always pointing down so it will always collide with something but here we are in third person which means we can look up onto the sky and since the sky has no collateral if there's no collision then the position simply defaults into zero zero so if i look up at the sky then goes all the way in there so if i click on the sky i'm placing an object in there that's not intentional so let's fix that there are two ways we can do it the first is simply testing to see if it collides with something and if there is no collision then we simply don't allow it to build or the simplest solution which is we can simply add invisible letters all around the world so to make sure that it always collides with something so let's go with that simpler approach just over here on the hierarchy create a new cube and just stretch it out and put it on the edges of the world okay just like that one in that corner another one that corner then rotate and put it on the other corner and finally the one up top okay so now the world is completely inside of these colliders now obviously this is not meant to be visible so just hide the mesh render and leave the collector enabled so just do that and now we have collaterals with no visuals and if we play okay so here i am looking at zero zero and now if i point the miles upward if there you go now the ghost stays in there and i cannot place it since it's too far all right yep great now the other issue is that placement can be a little bit fiddly when aiming at walls so let me place on some basic huts so i've placed these walls and now i want to place another far object right up there but due to how the grid position works it's based on actually pointing to the lower left corner so if i point over here you can see the wall the floor object is actually two by two so i cannot actually place it perfectly since if i move away then now it's no longer colliding so it doesn't work again since this is all working with colliders i can just put some invisible collaterals over here on the edges of the walls so into the objects let's go into the wall object and let's just make a cube and then just stretch it out okay so just like this this has a box ladder so let's hide the visual now it will still block the mouse and just make sure we don't make some invisible platforms just make this a trigger okay and now if i place down some four objects and place out a wall and i want to place a4 object on the second floor so if i aim upwards there you go that one is not correct so it's in there but as i move a bit up yep there you go now i can click in there place in there and everything works perfect and there you go i've got a really nice shoulder all right so with this now all that's left is really just to make this work in first person so it's really the same thing we just need to swap out the character controller so let me just duplicate the scene and make the other one in first person okay so here i've got the first person controller so same thing let's first get rid of these ones then drag the main camera the player file camera and the caption and just set the player camera root and just with this it should already be working all right so here i am walking around the first person now just use the buttons and place down a four object another one and so on place down a bunch of walls and nice door and now i can go in and there you go i'm inside my nice hut and now let's place things on the second level so fortune some stairs okay so now i can go up and place down things over here on the second level and maybe make another level alright so here i am i go to the third level and i'll look out the window so here you can see a really great example of just how useful these starter assets are so in 10 seconds you can easily get your characters working in third or first person so with all that here is the final system i've got my character and living around the world everything looks really nice now i can select what object type i want to place i can place down a bunch of four objects then i can place down some walls i can put up some stairs then place on floors on the second level and finally place down a nice door and there you go here's my nice hunt so i can go inside and yeah here it looks all very good i can go up to the second floor look out the windows and yep here i've got a really nice cozy hut as you can see this is a great building system very much like something you see in valheim and when using the first person controller it's just like something like minecraft or rust now you can go ahead and download the project files and build something on top of this also don't forget to check out core and the two events currently running make an awesome vehicle game for the core traffic jam and participate in the core invitational game jam for a chance to win from a 140 000 price point use the links in the description to download core for free and learn about the events alright hope that's useful check out these videos to learn some more thanks to these awesome patreon supporters for making these videos possible thank you for watching and i'll see you next time [Music] you
Info
Channel: Code Monkey
Views: 52,152
Rating: undefined out of 5
Keywords: unity building placement, unity build system, unity house tutorial, unity build room, unity house building, unity building game, unity building tutorial, unity construction, code monkey, city builder, unity house, unity strategy, unity city, unity survival, unity tutorial, unity game tutorial, unity tutorial for beginners, unity 3d, unity, game design, game development, game dev, unity 3d tutorial, programming, unity tutorials, third person, first person, fps
Id: gkCBCCKeais
Channel Id: undefined
Length: 15min 17sec (917 seconds)
Published: Thu Jul 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.