[MUSIC] So far we have a static platform game object. Of course, a moving platform would be a lot more interesting. There are several ways to achieve this. One way is to simply animate the platform using the unity animation tools. Another way is to move the platform through code. I'm gonna take the second approach in this lecture. Our objectives will be to create a moving platform parent game object. Apply a game object gizmo icon, so we can see it in the scene. Apply and setup the moving platform script. Set the moving platform based on the defined waypoints. And create a moving platform prefab that we can use in the future. Our moving platform is gonna be composed of multiple game objects. To hold them all together we'll use a pair of game object. So let's start by creating this empty parent game object. So on the higher key we'll use create, create empty, and I'm gonna go ahead and rename this as moving platform. So, this exists out in the world, of course. If I turn on the translate tool, you'll see where it's located currently. And, let's move it up where we might want the moving platform. So, perhaps it's going to be up here, a little bit above those base platforms. And we can go ahead and use our snapping tool to snap it right in the place, snap all the axes so it's centered right there. Or we can just make sure that position X, Y, and Z are set to whole numbers, at least X and Y in the case of a 2D game. But if this object is not selected, it's really hard to see out there because we don't have any visual representation yet. Of course we're gonna resolve that, but there's one nice feature in the editor that we can turn on. So, if I select the moving platform, and up here in the inspector there's this little drop down next to the cube. Representing the game objects. So let me click on that drop down, and we can select what are called game object icon. So let me go ahead and select the grey oval, and you can see what happens out in the editor, is it actually creates an icon for that game object. And represents it right there in the scene view, you don't see it of course in the game view. The players will not see this, but it's a very helpful thing for, things like this where it's hard to see, the object or even just for labeling things. So we can quickly tell that this is a moving platform, versus a stationary platform as an example. So the name on this icon of course, is based on the name of the game object in the hierarchy. So if you wanted to say something else, you would just rename the game object. So now that we have our moving platform parent game object set up, we're gonna go ahead and attach a script. So in our scripts, we provided a platform mover script, so I'm gonna go ahead and attach that to this game object. And let's look at the various properties of the component. So the first thing that it's looking for is the actual platform. That's gonna be moving. Of course, we've already made a platform pre-fab which we can use. What I'm gonna do is actually take my platform pre-fab and drag and drop it as a child of the moving parent game object. So let's drag it over and drop it right there on moving platform, so it becomes a child component. You can see that it's position, x, y and z is not centered on the parent. So I'm gonna change that just manually in the transform. So I'll set it to zero, zero. So it's right there. So if I zoom in, you can see that we've got that platform there and it essentially had kind of a label on top of it, because the parent is centered right there. So now with the parent selected, I could then define the platform in the platform mover component. So let me drag that platform right there. And we've got that set up. And the next thing in this component is the My Waypoints. If I open this cascade, you can see it's currently set at 0. And we can actually define however many waypoints we want our platform to move. So we can have it move from one waypoint to another, to another, to another. For purposes of example, let's go ahead and just set this to 2. So we've got two waypoints. So what the waypoints are looking for are essentially game objects that define the location of those waypoints. So what we can do since we have the parent object here, we can create an empty child of the parent. So let's go ahead and say create empty child. We're gonna go ahead and label this waypoint1. We've got the translate tool selected so I could actually move this out. Once again it's kinda hard to see. So let's go ahead use that game object icon once again, but rather than select one of the ovals which would name it, so if I do that, you can see wavepoint1 there, let's just go ahead and select one of the little dots. So I'm gonna pick the green dot, cuz it's gonna be my starting wave point, and you can see there in the editor, we've go that green dot. And I just pre-hand place this. You can see it's a -2.96. I might as well set this to -3. So it's right on, the grid system. Then let me go ahead and duplicate that waypoint. So I'll create a second waypoint and I'll name it waypoint 2. I'm gonna go ahead using snapping, holding down my modifier key, either Control on Windows or Command on the Mac. I can just pull it over here to maybe positive three. And it's got a green dot. I'm gonna go ahead and just change that to a red dot, to basically represent the sort of starting waypoint and the ending waypoint of my moving platform. So with those two way point to setup I can go back to my moving platform and look at the my way points under the platform mover component, and let's go ahead and attach those two elements. So way point one and way point two. And there is the icons even propagate into the inspector here. So we can set a moving speed. Notice we've got a slider here to define that, or we can type a number. Like five was the default, and we can see what that effect has. We have a wait time of one second at each waypoint, so once it reaches the waypoint, it's gonna stay there for a second. We could change that of course. And then we have an option to set it to loop or not. In this case, we want it to loop. We want the platform to move back and forth. So, let's go ahead and test this. I'll hit play. We can see our moving platform moving back and forth. I could run up there with sparty and I could jump on the platform. [MUSIC] And notice what happens. As it moves, I fall off. Or basically slide right off. So there's one other thing that we have to do to make this work. I need to tag this moving platform. So my character controller code basically looks for something tagged as moving platform. And if it is tagged as moving platform and I land on it, it makes it a child of that moving platform while i'm on it. So let me actually show you this as an example. So we've tagged most of our other things, but the moving platform is kind of a new entity here. We don't want to actually tag the parent because the platform is not actually in the parent, it's just the controller of the platform. So let me actually go into the platform itself, and we're gonna change the tag on this from just platform, and we're gonna make a new one. So let me go to add tag. And I'm gonna call this movingplatform. So I've got that tag created. Let me go back to the platform that's part of the movingplatform, and tag it as movingplatform. So with that done let me hit play again and let's test it. [SOUND] So if I jump on it now notice what happens. I'm following the platform. But if you look at the hierarchy, what is actually happening is I've got the little cascade on platform. And notice that Spardy is a child of that platform. As soon as I jump off [SOUND] I've got to have focus on the game. As soon as I jump off, notice that Sparty moved out. So it's no longer a child of a platform. So the code is dynamically changing the parent of Sparty. You know in this case it's not parented to anything and then when I jump on the platform it makes it a parent of a platform. So Sparty then basically tracks with the platform. And then as soon as I jump off. [SOUND] I break that connection and I'm no longer attached as a child of the platform. So the code is essentially dynamically changing the parent child relationship of Sparty and that platform. We'll look at the actual code details in a little bit. But overall we've got an awesome moving platform now that's very flexible. We can set up, you know, way points to be vertical, horizontal, as we've got here diagonal. We can have multiple way points. We can adjust the speed, the wait time and those sorts of things. So, since we've got that essential template of the platform set up let's go ahead and make this into a pre-fab. So, I'm gonna drag the entire moving platform. In the prefabs, and I've got that prefab that I'll set up, so I can make many moving platforms in my scene if I so choose. So let's go ahead and save our scene. [MUSIC]