Hey there, Gamers and game Makers!
In this week's blog, we're going to build on what we did last week where we made it so we could change scenes when we got to the end of our level. We're going to build on that by making a similar script that will reload the scene should you die by hitting a spike platform.
The main difference with this is that we aren't loading a scene by name exactly but rather reloading the currently active scene. So in other words, we don't need to make a bunch of these scripts or keep having to update scene names for it to work. It will simply reload the current scene. Let's get started.
As you can see, I've added a spike platfrom to my level and on that I've added a 2D box collider and set it to Is trigger. I've also once again created a new Tag in the project settings called reload and set the spikes tag to reload.
Next, create a new C# script called ReloadLevel and open it in Mono Develop. This script is going to be very similar to our load level script from before. Start off by adding using UnityEngine.Scenemanagement to make use of that library in this script.
Create a private void OntriggerEnter2D and set it's parameters to be a Collider2D and the tag reload. Now once again we'll be writing an if statement to check if the player collides with the object with the tag reload. If such a collision is detected we won't reload the scene by name like we did before for our change level script. We want to be able to use this script in any scene without having to edit it every time. So we'll have it reload the active scene by writing Scene scene = SceneManager.GetActiveScene(); SceneManager.LoadScene(scene.name);
Save the script and now add it as a component to the spikes object.
If you run the scene now and let the player fall onto the spikes, you'll see the scene get's reloaded. Try using this script on an object in another scene and you should see that it still works.
Until next time!
Hey there, Gamers and Game Makers!
In this week's blog, we're going to continue our Platformer Tutorial series by learning how to change levels. For example, you reach the end of one level and want to load the next.
To start off, I've created a second very basic scene as we'll actually need a second scene to change to in order to see our script working. I've also added the new scene to the build settings so that it will load.
As you can see in our original scene, I've added a sign or checkpoint of sorts. This will act as our end of level marker. The idea being that once the player reaches this point, the next level will load.
The next thing we want to do is create a new tag for our marker. I've decided to call mine loadLevel but it can be whatever you want. While you're setting tags, make sure the player is set to the player tag while you're at it.
Next create a new C# script called ChangeLevel and open it in Mono Develop. As we're going to be changing scenes, we'll need to import the SceneManagement library. To do this, go up to the top of your script and under the using UnityEngine; line type using UnityEngine.SceneManagement;
Now we want to create a public string called levelName. The reason for this is so we don't have to hard code the name of our level which if we did would mean we would need a script for every single level change which wouldn't be practical. This variable will allow us to enter the name of the level we want to load in the unity editor.
Now we'll create a private void of OnTriggerEnter2D. It's very important you don't forget to add the 2D as we are working on a 2D game. Otherwise unity will assume you are looking for the trigger event of a 3D object. In the parameters field type, Collider2D and the tag you assigned your marker. In my case it's loadLevel.
We then need an if statement to actually check for the collision. We're going to use the CompareTag method to compare the loadLevel tag to that of the player. If the marker detects a collision from an object with the player tag, then load the scene. Otherwise it's simply ignored.
Save your script and back in the Unity editor assign the script to your marker. Give the marker a 2D collider and set it to is trigger. Type in the name of the scene you want to load in the Level Name field of the script component. Save your scene changes and now if you play it you should be able to collide with the marker and load the next scene.
Until next time!
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!
Hey there, Gamers and Game Makers!
Welcome to the first in our Making a Basic Platformer Series. This tutorial series aims to give a basic introduction to the elements needed for a platformer game and how to make them. We're going to start by making our character controller that will allow us to move the player and make them jump. Let's get started.
As you can see below, I already have a very basic scene set up with my platforms and a weird little rectangle ghost dude that I made in Photoshop in a few seconds. Other than the sprites being placed in the scene there is no other set up done yet.
Ok, so the first thing we want to do is create a new C# script and open it in Mono Develop. This is going to be our character controller script and I'm going to name it as such.
You can go ahead and delete everything I've highlighted in the default script.
I'm going to start off by declaring some of the variables I'll need. I'm creating a private Rigidbody2D variable called RigBod that will be used as part of the 2D physics our character controller will have to make use of later. Next, I'm going to create three floats. JumpHeight which we will use as you may have guessed to control how high the player jumps. playerSpeed to control the speed of our player and a private float called playerInput which we will use to detect if the player is using the movement keys (arrow keys)
I'm now going to add a line in our start function that allows us to manipulate our players rigidbody via script.
Ok, on to the actual movement itself. We're going to start off by getting our player to move left and right. I do this by creating an update function called FixedUpdate that will set my playerInput variable equal to a get axis input. This simply checks if the player is pressing either the left or right arrow keys.
However, this does not actually move our player. We now need to set the velocity of our rigidbody. We also need to make sure we do not affect the Y axis with this particular input so at the end we simply say RigBod.velocity.y.
Once this is done, we can return to the Unity editor and actually apply this script to our player character and set some values. I'm going to start off by setting the speed to about 30 for now. We also need to add our Rigidbody 2D component to our player here. After doing so, make sure you tick the Freeze Rotation checkbox under constraints. We also need to add a 2D collider so that our player doesn't just pass through everything. Don't forget to assign colliders to the actual platforms themselves otherwise you might as well be jumping on a cloud.
If you play the scene now, you'll see that you'll be able to move the player left and right. However, you'll notice that the player is always facing the same direction. Not exactly the best if you want to move in both directions and have it look good. We'll use a little trick that will allow us to flip the sprite depending on which direction we want to move. Head back into the script.
First off, create a new bool variable called lookingRight and set it to true by default. Now, let's create a method that will actually flip the player sprite. Call it flipSprite and in the body of the method, set lookingRight to be equal to !lookingRight(equal to not lookingRight). Create a Vector3 Scaler and set it equal to the players local scale. This is the X,Y and Z values of the player at a given point in time in the scene. We then take the scaler of X and multiply it by -1. We thwn set the local transform equal to the Scaler. This method basically looks at the X value of the players transform when turning left. If the X value is 2 while facing right, to flip the sprite on the X axis we change this to -2.
This is all well and good but we still need to be able to check if we need this function and when to actually call it. In our FixedUpdate, add an if statement that checks if our lookingRight variable is equal to false and our playerInput is greater than zero. Basically this is checking if we are moving to the left. We then use the flipSprite(); method we created to do as you imagine and flip the sprite. We apply the same logic for turning right with an else if statement.
If you play the scene now, you'll see the sprite flips to face the direction you are moving in. Ok next up, we want to make our character jump. It wouldn't be much of a platformer if he didn't jump. We're first off going to need some new variables for this.
I'm first going to create a private bool called onPlatform which I will be using to tell if the player is actually standing on a platform. For this to work, we need something on the player that can be used to tell if the player is actually on the platform. Before we get to that, let's add the remaining variables needed. A public transform to check for a platform, a float to check radius and a LayerMask to determine what is a platform.
Back down in our FixedUpdate, we're going to set our onPlatform variable equal to Physics2D.overlapCircle. We need to tell it where to generate the circle and since we want this to be at the base of the player, we'll provide it with the platformCheck variable with .position. We then need to check the radius, so we'll provide that variable as an argument too and finally provide the LayerMask to determine the platform.
Back in the Unity editor, we'll need to create an empty game object to place at the base of the player. Make it a child of the player object and place it at the base of the player.The circle will now appear at this position. Make sure you assign that empty game object to the platform check component of the script in the editor. Give the radius an appropriate value. I'll use 0.2 for now.
In the Layers and Tags section of the editor you will need to create a layer that can be used to identify what a platform is. Once you have done so, apply this layer to all of your platforms.
Ok, time to head back into our script to finally get the player jumping. Start by creating an int variable called jumps. Then create an update method that will contain an if statement that will check if the player has pressed the space bar. What we are doing here is basically checking if the space bar has been pressed while you are on a platform. If so, once you jump we decrease the number of jumps you have so that you can't just keep jumping in mid-air forever. You can assign different values for the jump height, number of jumps and force in the inspector now.
If you play the scene now, you'll be able to jump around. Adjust your settings in the inspector until you get a jump that feels right for you. Finally, we are going to improve the overall smoothness of our players movement by preventing them from getting stuck on platforms. To do this, we will create a 2D physics material. Once you do this, set its friction to zero. Then assign this material to the players rigid body component.
Play the scene again and you should finally have a smooth playable 2D character controller. That does it for the first part of this tutorial series on making a basic 2D platformer. As always, feel free to get in touch if you have any comments or suggestions.
Until next time!
Hey there, Gamers and Game Makers!
It's been a while since I've done a update blog on what I'm doing since Hide The Body released. Well, since Hide The Body released on Steam and other platforms, my time has been divided between providing updates and new content for the game as well as finishing off year 1 of college as a mature student.
Now that college is out of the way for the summer, I've more time to focus on the next project. The next two projects to be exact. I'm currently in pre-production on both and as a result, I can't say much about them right now. What I will say is that I'm very happy with the unique concepts of both. I think they'll be something quite interesting given time.
I mentioned some time back that I'm also writhing a book related to Game Dev. I've still been chipping away at this but as you can imagine, college takes up a lot of time. I aim to cover a lot more ground on this over the next few months. In relation to the teaching of Game Dev material, I'm considering creating an online course for an introduction to Game Design/Development. The idea being that it will be more accessible than workshops you would have to sign up to and travel to. It also would be more affordable for those interested in it.
The next tutorial series on the blog will kick off next week. This will be a basic tutorial on creating a platformer game in Unity 3D.
That about does it for updates. Until next time!