Dan Kenny Game Design
  • Home
  • Portfolio
    • Game Design
    • 2D & Animation
  • Blog
  • About Me
  • Contact
  • Home
  • Portfolio
    • Game Design
    • 2D & Animation
  • Blog
  • About Me
  • Contact

Platformer Tutorial: One Way Platforms

14/5/2018

Comments

 
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.
Picture
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.
Picture
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.

Picture
You can start be deleting everything i have highlighted in the default script.
Picture
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.
Picture
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.
Picture
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.
Picture
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!
Comments

    Archives

    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    July 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    June 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    August 2014
    November 2013
    September 2013

    Categories

    All

    RSS Feed

© COPYRIGHT 2020. ALL RIGHTS RESERVED.