Bolt Quest - "How do I move objects in Unity?" [Set Position, Translate, and DoTween]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi i'm jim and bolt quest is a series where i answer your questions from the bolt discord the first question comes from skylight gaming who asks has anyone got an idea of how to get the transform of an object i'm trying to make a point where the player gets teleported great question skylight i'm gonna talk about three different ways to move objects in unity with bulb this is the final effect that we're going for if you want to follow along and download the package below but you could use any project that you're working on so I just have a flat white material on all these little buildings from Kyoto and I made prefabs of the Sun and the moon and you can see that I have lights childhood to each of those objects and what I want to do is have the Sun start out below the horizon and then shoot up into the sky so first rather than scripted this on the Sun object I'm going to do it a different way so right click and do create empty and then let's call this game object sky controller because we're going to control the sky and I already have bolt installed so I'm going to go to add component and add a flow machine then I'll click new to make a new macro and I'll save this as sky controller and I'll give you my completed graph it will be sky controller bolt quest tutorial alright and I'll save that okay so now have the graphs here in order for the sky controller to access the Sun and the moon will need to have variables that reference to those game objects and since I don't have the variables window here I'm going to go up to window and get graph inspector and variables and then I'll just stock those in the corner so there's different variable choices graph variables which are just for this graph like internal circuitry object variables if you add multiple graphs on this object want me to know about the same things and then scene variables for example like whether it's day or night is maybe something a lot of objects would want to know about object variables are visible in the inspector on the game object and that's useful for movement scripts because you can drag any object you want into that variable slot and be able to move it so I'm going to start with making two variables one for the Sun and I'll make it of type game object then I'll make another one moon and also make that of type game object and then I'll drag the Sun and the moon game objects into both of those slots the first way we'll do this is with a node called set position so right-click and type set position and the one that we want is transform set position transform is referring to the transform component on the game object you can see that word transform is right there now a word like transform might make you think of a transformation like a werewolf and if I threw you banana you wouldn't say that the banana transformed to you but our objects are really just a bunch of data and every time when you scale something or rotate or move something that original set of data is being transformed into a new set of data based on the directions we're giving it so unity and other programs have named this component for the mathematical stuff it's doing and I've included a link below to a Khan Academy video on transformation and mathematics okay so all connect up start and then I'll grab the Sun variable which automatically makes a get variable unit and we want to connect that up to where it says self because self is the sky controller and we don't want to move the sky controller we want to move the Sun object I'll put the Sun below the horizon and I can see that it's at 50 on the Z so for the set position I'll put it at 25 where it was at and then I need to put 50 on the Z so it stays at the same point on that z axis all right I'll press play and you can see this work so it automatically switches to the game view and since this is happening immediately on start we don't even get a chance to see the Sun at the horizon but if you look you can see that it is at 25 and 50 transform set position is doing something automatically for us skylight gaming's question was about gay the transform from a game object so let me right click here and type get game object get transform in programming this is what you need to do so I'll connect up the game object we get the transform from the game object and then we set the position on the transform so if I press play you can see that this also works but we don't need to do it because transform set position is getting the transform from the game object for us so I'm going to show you quickly a scenario where you do need to get the transform from a game object so on keyboard input I'm going to once again set position transform set position and then I'll connect that up and I'll put the Sun in the thing we want to move and then the moon is where we want to move the Sun to let's see if this works ok you can see that we get an error message here cannot convert Unity engine game object to Unity engine vector3 ok so I'll stop the game and right click on the connection to sever it and then we're going to drag out the connection and let go and type transform get position and get the transform get position unit now check out the slight difference between the icons these two icons match their vector three icons and that is a transform icon which matches that transform icon right there to get the moon's position we have to go into the moon's transform then we can get that vector three and set this on to it rather than you start let's use that on keyboard input so I'll right click and then just type on key and what we'll do is set it so that we can change the position of the Sun up or down so up arrow will send this on up and then down arrow will send the Sun down so I'll just duplicate everything with ctrl D and on the duplicated graph let's change it so that it the Sun goes to zero and that that happens on the down arrow ok I'll press play and you can see that because I'm in the scene you it actually moved the scene view forward I'm gonna click on the game view and now our arrows will work up down up down all right we can use set position for teleportation or we can use it to move things gradually I'll make some space here and then let's get an ad unit ad unit for vector 3 and then we're also going to need to get the current position of the Sun all right click Add unit and type get position transform get position and then I'll get the variable to the Sun game object that's what we're getting the transform and the position from so we're getting the position of the Sun and then we're going to add to it on the y-axis and we'll just add one and then we need to put that into the set position okay so let's try this out click here and there is the Sun slowly appearing we can do this on a keyboard input or we can do it on update and I'm gonna speed through that real quick and so I don't go on for like 30 minutes so bring back the update unit and we'll add a small amount to the Y maybe point zero one is too small I'll do 0.1 and there's a Sun rising now there's nothing stopping the Sun so it's just going to keep rising and rising and will die in the cold void of space so I'm gonna stop the game before that happens okay and to just review the graph a little bit what we're doing is we're getting the position of the Sun every frame and we're adding a small amount to it and then with that new vector3 we set the Sun to that position so that the next frame the Sun is at the new position which we get and add to again and set again and we keep doing that getting adding and setting over and over now if this feels like a lot of Units to just move something up I totally agree with you let's try another way of doing this so right click and type translate we have a couple different Translate units let's just use the one without the relative to just the XY and Z so again we want our let's do the moon this time so bring the moon out and I'm gonna disable the Sun with my shortcut to disable things it's the same as doing that and I use the tilde key for the shortcut for that it's really fast so I'll enable the moon and let's set the moon to zero okay so let's try translate so we're gonna translate the moon two meters on the Y on keyboard input and what you'll immediately notice about the moon is that it moves diagonally translate by default works on the local orientation rather than the global so you can see that if you press the X key and if you press the X key again it goes back to global so I thought it looked good rotated on the z axis a little bit I'll make that zero and now since it isn't rotated it moves straight up wherever I rotate the moon it will always move up on that local Y axis but what I want to do is move it up even though the object is rotated so I'm gonna get translate relative to so with the relative to we can change it to be relative to the world rather than relative to itself that way it'll just move the object up in the global Y no matter how its rotated and of course we can use update instead so I'll move it up a little bit every frame so we don't need to set the position translation does that automatically for us now since we're using update we're moving this point one every frame in our frame rate fluctuates a lot so in order to get the movement to be more consistent let's get a per second node and that first one and this way we can specify how far we want the Sun to move every second regardless of how fast or slow our frames per second is so all make it be five and if you've heard of time.deltatime before that's what's going on under the hood you can see that that number changes there but the movement is consistent and for the sake of time I'm just going to cut in with a completed graph on how to set something up to where it stops when the object is getting less than a certain distance from a target so you can see we have a vector3 distance node and the distance is getting less and less and less and then when it gets less than 1 the branch will flip to true and nothing happens after true so the branch and the less than or equal to is saying are we there yet no translate are we there yet no translate are we there yet oh we're there okay we're there don't translate anymore true nothing happens now there's a cleaner way to do all this so I'm going to delete all these units and then go to the asset store I'll press shift in space to full screen the window and then search for du tween there's a pro version of du tween and a free version we're going to use the free version I've already imported it but I'll import it again and you need to click setup to tween and then after this compilation process click apply then we need a tell bolt about this du tween package so that bolt can generate the units so go to tools bolt and setup wizard and then click Next I'm using human naming assemblies are libraries of code that we can use so scroll to the bottom and there there's a little plus sign you can click it and type du tween and then also add du tween editor and then next types or data types so we've already used data type vector 3 for example so at the bottom add all of these du tween related data types we won't be using all of them but if you check the do tween manual these cover most of what I think you'd want to do and in the next video we'll be using tween parameters then click generate when you all those all control-d duplicate the on keyboard input and changed the key to the up arrow and then once that's generated all I need to do is type do move and this is do tween stuff so let's do doo doo move why all right so I'm gonna connect that up it's referencing itself which we don't want let's do let's go back to using the Sun so I'll plug this on in because that's what we want to do move and the end value is the Y so let's make it 25 and then the duration let's do one second and then rather than the set position and Sun here I'm gonna do this again and I'm gonna do a do move on the so the duration one again and then the N value will be let's send it below the horizon to negative 25 I'll press play all right let's try it up down cool so I'm gonna time-lapse this for the moon I'll do the flip of what we're doing to the Sun the moon will go negative 25 when the Sun is rising and then when the Sun is descending the moon will go up 25 now for the final cartoon effect I'm also changing the sky box and the animation has a little bit of personality to it and I'm gonna cover that in the next video thanks for watching and if you haven't yet joined the community on the boat this court and let us know your quest
Info
Channel: Home Mech
Views: 8,254
Rating: undefined out of 5
Keywords: programming, unity, bolt, game development, game design, tutorial, programming tutorial, c#
Id: 5Hte52kj8ao
Channel Id: undefined
Length: 15min 15sec (915 seconds)
Published: Thu Jun 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.