For this project, I wanted to create something different. This platformer somewhat tied back to overall aesthetics of the cookie clicker clone. This was one of the hardest projects I’ve worked on, a lot of additional functions I wanted to include didn’t work well with the code for the player movement such as the respawn function and even the death function. So I decided to just focus on the basic mechanics for the game.
Scripting: Player Movement
To start off with, I added some variables for Unity to call back to when different movement inputs are used. I want to be able to control the player’s speed, jump force as well as edit the sprite’s movement with moveInput. The Private Rigidbody 2D and Sprite renderer help link back to the player sprite.
I also added collision and a RigidBody2D component to my sprite as well as a collsion component to my platform tiles so my sprite didn’t fall through the platform.
In addition to this, I added a physics material to the RigidBody so the player doesn’t get stuck between the platforms whilst they were colliding with the objects.
I started by adding Get Components in the Void Start function in order for the script to access the GameObjects needed for the code. In this instance, it’s for the character sprite.
For the move input, I wanted the character to only move horizontally so the moveinput would only register the left and right inputs.
The use of ‘GetAxisRaw’ command means that the game will automatically and immediately reflect on the change of the axis value, only returning to 0, 1 or -1.
Since, I made the Rigidbody as a variable, I was able to add a velocity function which allowed the movement input to work alongside the player’s speed, however since I didn’t want the velocity to affect the players left or right movement, I made sure the velocity code only used the object’s current y value.
Since my sprite was going to move horizontally, I wanted the gameobject to flip every time the player switches between a left and right input. So if the move input on the axis was less than 0 then the sprite would flip it’s axis.
The next mechanic I had to focus on was the jumping mechanic, I needed the character to also react with the ground however, this would be the most difficult function to implement into this game as it interfered with other mechanics later on.
Scriping: Player Movement Part 2: Ground Check and Jump
The purpose for Ground Check is for the sprite to only jump when it’s made contact with the ground, meaning that the sprite wont collide with the ground. So I created a emptychild object called obj_GroundCheck and placed it near the sprite’s feet.
Heading back to the Player movement code, I added more variables in regards to the ground check, adding comments to remind myself what they meant. These variables will make sure that the groundcheck object and the player can detect what the ground is based on collision.
These variables are then used to set a new booelan string to ‘true’. If the radius of the circle overlaps with an object set as its ground layer.
For the jumping mechanic: I added a public integer variable called Jumps
this was originally to limit how many times the player can use the double jump – however, later on I found out that there were some issues regarding that.
So I added an “if” statement where if the player was on the ground then they’ll be allowed to jump, originally I had set the jumps to 2 to enable double jump.
then I, put an key input for the jump mechanic to be space and if the number of double jumps were greater than 0, the player would be able to jump but it took 1 double jump from the amount given.
However, this didn’t work as I later found out when testing the game.
This is the part where I started experiencing issues:
Whenever I started the game, the sprite would only be able to jump a certain amount of times from the jumps variable. This essentially meant that my double jump didn’t work. I tried rectifying this by adding the ground layer to all my platform tiles but it didn’t work. So my workaround was to limit the amount of jumps the player has to 100.
I wanted the jump force to be low so it forced the player to be generous with how many jumps they were inputting at once. This is potentially an interesting mechanic for future updates as it adds strategy in the gameplay. For the player to know how many jumps they have left, I could implement a jumps counter and a mechanic where the score is based off of collecting all the coins in a levels as well as how many jumps the player uses.
For the currency and the respawn functions, I got the code from the same source. I was only successful in adding the currency mechanic unfortunately as the respawn function didn’t work (more explained in the next section).
I started by adding a level manager script, this was to create a space where external mechanics would be implemented.
To start off with, I wanted
These two functions allow for the level manager script to be called within other scripts in Unity. This was incredibly helpful as it prevented clutter in different scripts.
I added two public variables, the currency and the public text. From my work with the cookie clicker project, I knew that I needed to use UI elements in order for the text to update every time the player collects currency within the game.
In the public void function, I also added another integer which was the amount, the amount would serve as a way to increase the currency count as well as update the text to show how much the player has collected through the UI text, much like the counter mechanic in the cookie clicker game.
For the UI, I created a UI panel. I adjusted the canvas so the panel would appear at the side of the screen and I made sure that it adjusted to the screen size.
The UI text was also adjusted so it linked with the panel
Next, I wanted to focus on implementing the currency into the scene, so I created my own ‘demon coin’ sprite and added a circle collider to them so they would collide with the player, making them into a trigger. Then I made them as a prefab, so I could implement multiple copies of them throughout the level.
I created a seperate script called ‘scr_CurrencyPickup’ so I could add code that would basically make the coins disappear when the player collides with them as well as add to the amount in the currency UI. I also referenced the Level manager script so these two scripts are connected and player the increase currency function.
Finally, I linked the UI components to the variables in the Level Manager and so when my game starts playing, the text updates everytime the player collects a coin.
Attempted Scripting: Respawn
Unfortuantely, I deleted most of the script for the respawn function because it was interfering with the way my game worked.
The code I used was similar to the enemy respawn function in my top down shooter game since I used the instantiate function and the collision. I added variables to the Level Manager in order for the character to respawn at a particular point.
However, whenever I attempted to have my character respawn, they would fall off the map, I tried using the platformer sprite prefab instead and it didn’t solve anything.
So I wanted to add another transform variable for the instantiate function to respawn: the groundcheck object to see if it worked but Unity couldn’t instantiate the object. It was a learning curve but I want to be able to add a respawn mechanic or a game over screen in future updates to the prototype.
In summary, this project was incredibly hard to work on, I wanted to add more features to the prototype but they either didn’t fit or they made the game unplayable. The currency function was the only one to actually work for the game which I was incredibly proud of since I used code from cookie clicker to have the UI work for the game.
I learnt a lot when it comes to creating a platformer game, however for future updates, I want to see if I could use something other than groundcheck for my jumps to work as it seems to be the cause of why my respawn mechanic didn’t work.
I also want to create a larger level for the game with more enemies but since I struggled with the jumping and groundcheck mechanics, I was wary about adding more mechanics into this prototype.
Sources used for Extra Mechanics:
Software Used: Paint Tool SAI, Unity 2D, Microsoft Visual Studio