Video: Getting Started With 2D Game Kits – Controlling Platforms With Switches [6/8] Live 2018/2/21
Next, we'll take a look at adding another moving platform, but jazzing up its path a bit and activating it with a switch. Firstly I'm going to move Ellen and the Camera again. So, I'm going to select "Ellen" and CTRL + click on Cameras, make sure the Transform tool is enabled, with W, and just move her past this. Now, Spitter isn't going to give us any trouble here, because, if I select Spitter, his field of view isn't where I am, so he's looking over here in this green area, and I'm just past that, so he's not going to attack me in any way. So I'm going to be safe while I'm testing this mechanic. Let's add a moving platform, and just like before, in the "Interactables" folder, here's the "MovingPlatform".
And I'm just going to click and drag it here. That looks about right. And, just as before, we have its direction and destination, except there's just one here and I want to add another direction and another destination. The way I can do that is in the "MovingPlatform"… and in its component we can see a setting for "Add Node". If I click on this, this adds another little Transform gizmo right here in the Scene view. And if you pulled that away, you can see another dotted line So if I just leave it there and then go back to preview position and move my slider, you can see that it'll have another destination: once it reaches there, it changes direction. I actually want to put this here… and preview that again. And that's going a bit too far, so I'm going to move it here. That should be OK. Just about… almost… That should be fine. Now I can add multiple nodes, and if I keep clicking "Add Node", it creates more of these little parts here. But I want to get rid of these, because I just want the two, and the way to do that here is: here is your nodes list, just underneath the button, and if I click "Delete" "Node 4" and "Delete" "Node 3", I'm just left with the two that I had before.
So that's all well and good, and it's going to start moving as soon as I press Play, which is fine for now. But what if I wanted to make it a little bit more complex and trigger it via shooting a switch? I can set this up fairly similarly to how the door and the pressure pad is set up. In the "Interactables" folder, in the Project window, I have this "SingleUseSwitch", which means it's a switch that I can just use once, so as soon as it's been shot, that's it: you can't shoot it again, it doesn't trigger anything. And if I press Play now… and since it's quite a bit small, I'm going to have to press S to duck and then O to hit the switch. It doesn't really do much, it just lights up, it plays a sound and plays a small particle effect, but it doesn't affect the moving platform, just like the pressure pad didn't affect the door. So we need to set this up and call another event. I'm going to exit Play mode, and now, with the "SingleUseSwitch" selected in the Hierarchy, let's scroll down in the Inspector and take a look at this "Interact On Trigger 2D".
This will take its Collider, and as soon as something hits the Collider, it will trigger these "On Enter ()" events, so when something enters the Collider. You'll notice that it also has a "Layers" and it's set to the "Bullet" layer, so every bullet Ellen fires with her gun is set to the "Bullet" layer, and that means when she shoots something, it's on the "Bullet" layer and that will trigger the "On Enter ()" events. In these events, when a bullet goes into the Collider, the Sprite switches and it plays a sound and some particles. And we've got some empty event slots here for you to fill into. Let's get the platform moving when we shoot this switch. We already have an empty event here, so we don't need to press the "+" button.
I'm going to grab the "MovingPlatform" and, because it's a duplicate name, it's added a "(1)" next to it, so I know this is the second one that I've added in here. If I click and drag on the "MovingPlatform (1)" into the empty slot, now it knows the triggering event for it, but we haven't set a function to call just yet. If we click on this, we can see that there is a "MovingPlatform". It has taken the components that are on the "MovingPlatform" and put them in here. In the "MovingPlatform", we have a "StartMoving ()", and if you remember, that is the same as the "Start moving" option we have. If I go back to this "MovingPlatform (1)", just to quickly show you in the "MovingPlatform (1)" there is the "Start moving" button, and it's currently on. We want to turn this off first, so when we press Play it doesn't move. So here, it's not moving.
But just because we've dragged the "MovingPlatform (1)" into the event doesn't mean that it will start moving. Because we haven't set a function to it. So right now it's not doing anything. I'm going to exit Play mode and go back to the "SingleUseSwitch", scroll down to the "OnTriggerEnter" sorry, the "Interact On Trigger 2D" – that's a different component right there – and then, in the function, back in the "MovingPlatform", we can put "StartMoving ()". And this will set "StartMoving" to "true" and it will enable it. So, if I press Play… and shoot the switch, the platform starts moving, and it will move in the direction that I said earlier. And I'm going to exit Play mode..