Super Units! Visual Scripting in Unity! (Bolt)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to learn what are super units in bolt which is unity's official visual scripting tool they allow you to essentially create little modules with custom inputs and outputs that you can then reuse in your various visual scripts let's begin [Music] hello and welcome i'm your code monkey and this channel is all about helping you learn how to make your own games with in-depth tutorials made by a professional indie game developer so if you find the video helpful consider subscribing okay so a while ago i did a video covering the basics of visual scripting in unity that video covers a quick getting started so if you haven't seen it yet check the link in the description at the end of that video i asked what you would like to see and lots of comments said a complete game so that's what i've been working on now right away as i started looking into how to make a complete game i came across a need to organize my visual scripts and that's when i ran into super units super units are essentially functions that you can create in visual scripting you can define custom inputs and outputs and then reuse that super unit in other visual scripts this video is made possible thanks to these awesome supporters go to patreon.com unitycodemonkey to get some perks and help keep the videos free for everyone alright so let's try it out as a demo i have this very basic setup so this is meant to be my character and it's meant to be in a top-down shooter so the first thing i want is for this character to look when i move my mouse so we're going to first make the visual script to look at the mouse and then we're going to see how we can convert that into a super unit in order to keep our player macro nice and organized okay so let's make our player visual script now first select the player and add component and we're going to add a flow machine then here we have film for macro so let's create new and let's call this the player macro alright so here's the flow graph so the first thing we need is the mouse position now we can grab the mouse position with the node input mouse position so this outputs a vector 3 with the mouse position and here for testing just to see what value comes out of here we can connect this and let's connect to analog node right so just a basic debug log and we connect it to our update okay let's test and yep here we can see the vector3 that is being outputted by this input mouse position and as i move the mouse around the scene you can see that it moves so if i'm on the lower left corner you can see the vector3 is pretty much on zero zero and up here on the upper right corner it's essentially at the size of this window so in this case 800 by 400 pixels however as you can see that is the screen position not the world position so we need to convert that now the way we can convert that is by adding the node screen to world point let me just right click here in order to make this full screen so we have a bit of a working area and again here on the graph inspector we can see exactly what this does so it transforms a point from screen space into world space and the inputs are the basic flow it inputs a screen space position and it also takes a camera so in this case we need to grab the main camera so we get the main camera connected into down then for our flow we connect into this flow and for the position we use our mouse position alright so this should be correctly calculating the worm position based on the mouse position okay let's see the script here we can visualize all the flow and we can see on the left corner yep in there we can see the screen position and in there we can see the world position now the character is on zero zero and if i go there yep there you go it's pretty much on zero zero okay so it seems we have a correct one position however in there you can see that the z is at -10 that is u2 where the camera is positioned so when dealing with our math we really want the set to be at zero so let's fix that here we can just drag our vector3 and click on vector3 and let's expose the entire vector3 and in this case we want to take the x and y and set these add to zero so afterwards let's make a new vector3 so we just use this connect the x connect the y and there you go here we have our output so let's drag this one here this one here connect the flow in there and the flow in there all right so our output should be the world position of the mouse with the z set to zero and here we can see all of our nodes that work all the flow going from left to right and we can indeed see that our position on the output in here it is indeed correct so right down middle is on 0 0 0. so with this we have all the nodes that we need in order to get the mouse warm position and it all works perfectly however we have all these nodes inside of our main player macro so if we keep this up then at the end this visual script will become completely massive by the time we add everything so that's where super units come in now there are two ways we can do this so here we can right click go into nesting and create a brand new super unit and here is the node and we can double click on it and there you go we go inside our super unit so this is one way and doing it this way it gets saved directly inside of our first graph so up here we can see where we are so we are inside player and then inside the super unit so if you click on the player we go back upwards one level so if you have some behavior that you only use inside a single macro this would be one good way to do it however if you want to reuse some general behavior you should go with the second method of making a proper separate macro so in this case this is some pre-general behavior so let's do that here in the project files i have the macros folder and let's simply create a new bolt create a new flow macro and let's call this the get mouse world position and now let's copy all the nodes that we developed in here so let's copy all these select the get module on position here we are in this flow graph and just paste our nodes okay so we have our super node and now if we go back into the player macro let's delete all of these nodes and instead let's just drag our getmouser on position macro and drop it in here and yep it gets added as a super unit all right so far so good however right now this super unit isn't doing anything now in theory it's grabbing the mouse on position but right now there's no flow input and no flow output so we can't see anything so let's go back into our super unit and in here you can also see that all of our nodes are dim meaning they are not being run so in here let's add a new unit go into nesting and we're going to add an input node so here's the input which again as the tooltip says fetches input values from the parent super unit for this graph and in here on the graph inspector we can add some settings so we have control inputs and value inputs now let's first add the control inputs and we need a key which is a unique name so in this case let's just call it enter so this will be our interflow and just hide the label since this is just a basic flow port and then down here we can also add some input values now in this case we don't care about any input values all we care about is the input flow and then we're going to work on some output values now here visually on the input we can already see that we have a flow input and this is what we're going to connect in order to make our code run all right so now for the outputs again let's add a unit let's go down into nesting and add an output node so this passes output values from this graph to the parent super unit and first we add a flow output so in this case let's call it exit and again let's hide the label and now in this case we're also going to have a value output so let's click on this now for the key let's call it our world position and now we need to select the type so in this case we're going to output a vector 3. so just select factor 3 and yep there it is so here's our output node and we just connect the ports pretty simple all right so just like this now our nodes are all correctly set up so we have a self-contained macro with our inputs and outputs and now we can go back outside in order to visualize the player macro and in here we can already see the super unit yep it has an input and two outputs so we connect the update into this input and on the output let's just connect it directly in there all right so there it is so everything should be working let's test and yep everything is working perfectly so we have our update event passing in the flow into our super unit then the super unit does its thing and it outputs our desired one position vector which then goes into the log just so we can see it so as i move the mouse around yep you can see that it does indeed output the milestone position all right so that's the basics for super units essentially you put behavior on its own unit so that you can then compose multiple complex units into one complete unit okay so let's expand upon it we have the mouse run position now let's rotate the player towards that so we need a couple more notes so the first thing we need is to calculate the direction so for the direction we take the world position then we're going to add a subtract node and we're going to subtract it from the actual player transform position we get the transform position and we use this one in there all right so over here we have direction then with the direction we want to normalize it so just drag this and we can use the normalize okay so we should have a direction normalized so again let's test and up here we can see our normalized direction so as the mouse is on the right if there you go one zero zero and if it's above zero one 0 all right great so we have direction here now we need to convert it into an euler angle so for that we can use a new node go into math and we're going to use the attan 2. now here we need the x and the y so let's split this vector all right here we have the tan two so this will give us the angle in radians now let's convert it into degrees so take the math read to degree multiply it by our radians and yep over here we have our euler angles and now in here we can just simply apply it so the node transform euler angles okay so we create a new vector 3 with our euler angle rotation on the zen since we're working in top down 2d and we just apply it to the transform oiler angles alright so just like this our character should be looking at the mouse let's test and yep there it is there is indeed the character is pointing towards the mouse so as i move the mouse yep the character rotates alright awesome so you can see everything is fully working but you can also see all the tons of nodes that are required to make this work so if this one wasn't inside of a super unit this would be even more notes so once again let's clean it all up with another super unit so let's create go into bolt create a new flow macro let's call this get angle from vector and we're going to copy over here these ones to convert the vector into an angle now once again let's set the inputs and outputs so yep there it is we have an input with an input following input vector then we do our calculations and we have an output with an output flow and the output angle and now back in our player macro once again let's get rid of all this and instead let's drag the get angle from vector we input the flow and put our normalized direction vector and then the output outputs the flow and the output angle all right let's test and yep everything still works perfectly so just like this you can see how useful super units are they help you manage complexity as your visual scripts become more and more complex if we then use super units then this player script would have been massive however using them we're using one in order to get the milestone position and another one to calculate the angle from a vector so with that all this relatively complex behavior fits in just a handful of nodes and again we can reuse this on anything so on any other visual script where we need to get the mouse on position yep we can reuse this super unit and whenever we need an angle from vector we can use this one you could take it one step further and just group all these into another super unit by calling it something like rotate transform towards mouse position alright so these are super units an excellent tool to help you manage complexity when using visual scripting this is the main way that can help you build complex scripts without making the whole thing a massive spaghetti code alright so with this covered i'm going to continue working on making my top down shooter completely without using any code if you found this video helpful consider liking and subscribing this video is made possible thanks to these awesome supporters go to unity patreon.com monkey to get some perks and help keep the videos free for everyone as always you can download the project files and utilities from unitycodemonkey.com subscribe to the channel for more unity tutorials post any questions having comments and i'll see you next time you
Info
Channel: Code Monkey
Views: 34,237
Rating: undefined out of 5
Keywords: bolt super units, unity super units, unity visual scripting, unity bolt top down, code monkey, unity bolt tutorial, unity bolt 2020, unity bolt, visual scripting, unity visual scripting 2020, unity visual scripting free, unity tutorial, unity game tutorial, unity tutorial for beginners, unity 2d tutorial, unity 3d, unity, game design, game development, game dev, unity 2d, programming, coding, c#, code, software development, learn to code, learn programming, unity tutorials
Id: o3GTDHBbUuQ
Channel Id: undefined
Length: 13min 9sec (789 seconds)
Published: Wed Aug 05 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.