Hey there, Gamers and Game Makers! In this week's blog, we'll continue our basic platformer tutorial series by looking at how to make a one way platform that allows the player to jump onto it from below and then stay on top of the platform. Let's get started! As you can see, I still have my scene from the last tutorial set up. The only difference is I've added another platform that's a different color. Right now this platform acts exactly the same as the others. The first thing I'm going to want to do is to select my new platform and add a new component to it called a Platform Effector 2D. After you have added the component, go up to your Box Collider 2D component and tick the used by Effector check box. That blue dome you see is what determines where collision will be detected. As you can see, anything below the dome (below the platform) is ignored meaning we can jump from below and land on top of the platform where collision detection will kick in. You may need to adjust the radius and angle of your Effector depending on your platform. If you play the scene right now, you'll be able to jump from below and land on top of the platform. This works as you'd imagine but what if I want the player to be able to get back down? The collision is now active and I can't fall off the platform unless I go to either side and jump off. So, how can I make it so I can have the platform "drop" me? What we basically do is have the collision detection dome of the Effector rotate by 180 degrees on a key press. To do this, we need to create a script to modify it. I'm gong to start by creating a new C# script called OneWayFlip and open it in Mono Develop. You can start be deleting everything i have highlighted in the default script. Start off by creating a private variable of type PlatformEffector2D and call it myEffector. Then create your start method and in the body of the method set myEffector equal to GetComponent and supply the PlatformEffector2D to it. Now in my Update function, I'm going to create two if statements. the first is going to check if the player presses the down arrow key while on the platform. If they do the offset is rotated by 180 degrees allowing you to fall back down. Now the problem here is because we have set the offset to 180, we can no longer jump back up. So we use the second if statement to check if the jump key (in our case space) has been pressed indicating a jump which will then change the offset back to zero. Now, back in the Unity editor drag the script onto the platform to add it. If you play the scene now, you'll be able to jump up onto the platform and fall off of it by pressing the down arrow. That does it for this week's part of the basic platformer tutorial. As always, feel free to get in touch if you have any questions or suggestions.
Until next time! |
Archives
April 2019
Categories |