My next plan is to develop the scripts and functions the way I envisioned them to work in the game. Having the sole player movement script didn’t feel like enough, so I got to work. Before I could do any more scripting, I had to make part of the stage look nice, so I added decorative assets like rocks and bushes to the scene for it to look nice.

I also expanded the level a bit more to give the player some freedom and to maximize the jungle stage a little more for additional enemies and traps along the way.

Now it was to add a Health System, This one was a little tricky than the last, mainly due to the script not being covered in the weekly activities. I mainly had to look online for a video that would help me make this script. I checked that the image script was attached each heart for them to work.

I had to make sure the amount of hearts I needed were in the script too. I went for 3 hearts, as it is a standard number for the game I’m making.

I dragged all of the hearts into the array I created in the script. This will allow the game to show the amount of health and number of hearts present.

The script allows the player to have 3 full hearts upon starting the game. The damage script will be added later.
As you can see, the image below details 2 full hearts and 1 empty heart, an indication that the script works. I tested the script to make sure too.

Now it’s time for me to create some enemies. I made a second spritesheet just for these guys. The four enemies are Rats, Bears, Jungle Cats and Snakes. Their first sprites are their idle animation and the second is their walking animation. I plan to modify the script to make it so that the two sprite change repeatedly while the enemy is patrol. Depending if I can get the player character to show its shooting animation upon hitting the mouse button, then I’ll just stick with the idle sprite.

These are the enemy sprites in the Unity Project Window, before and after being sliced.

To be honest, this took me a while to perfect, but I got there in the end. I created a new script called Patrol and wrote the code for the enemy to wander around the 2D stage. There are multiples ways of doing this, like attaching a Raycast which I attempted, but made the enemy glitch upon hitting play. The technique I used involved Waypoints that act as a barrier that sandwiches the distance the enemy could travel before flipping sprite. I set the move speed to 2 for a comfortable speed and tested the move right option by checking the box multiple times. Yep, it works!


This script allows the enemies to move left and right within a distance before moving back. I created a Tag called Turn and set the Waypoints to that tag.
Whenever the player moves, the main camera sticks to the same position, which can be a little jarring. I found a way to stop that. I opened up the Package Manager and searched for the Cinemachines plug-in and installed it. for a better camera experience.

The Dead Zone is where the player can move freely without the camera scrolling in all directions. I made sure I set the adequate width and height of the Dead Zone for my character to fit inside.

Here is the initial Dead Zone, note that it’s smaller and doesn’t necessarily fit the character within. After some tweaking and playtesting the camera for myself, I found this size to be decent enough for the players to view the ground assets after jumping otherwise it’ll just show the top half of the tiles.

Next up was the script that will change the player sprite upon performing certain actions, like shooting and jumping. I created a new C# script and added the sprite renderer and sprite variables that I would need.

I admit, it was a little tricky at first getting the script to function properly as I hadn’t executed this yet, so I didn’t know what to expect. I eventually followed through the steps of creating said script and it ultimately turned out to be simple and easy to do. The image below shows an example of my progress with the script.


I added this function myself with no problem. What this basically does it that when the left mouse button is clicked, the sprite changes for a split second before reverting back to the playerIdle sprite. The change is executed in an if statement, which is why the spriteTimer is a float variable. With this addition, the game would be more engaging for the audience. Here is the finished script with the aforementioned shooting sprite along with the bonus playerJump sprite I also added in.


Now it’s time to add in another script. This one would be for the bullet shooting when the left mouse button is clicked. The above image is the script for the individual bullet, but I had to create another script for the player weapon. Here is it.

The script above basically allows me to instantiate a bullet upon a single click of the left mouse button, something I had envisioned beforehand. The Player Shoot sprite from earlier would also be triggered, effectively going around the route of animating the whole attack animation if I could. The reason I went for this method is because I would have had to learn how to animate and make the scripts for them, which I ‘m going to be honest, it would take alot of time for me to understand it to the best of my ability.

I went back to my player movement script and created a new variable named [HideInspector] public bool isFacingRight so it couldn’t be shown in the Inspector View (Muddy Wolf Games, 2020). I also created the lines of code you see in the image above in the update function. Now the next step was to make the bullet sprite a prefab so it would function properly.

Inside of the script’s changeable variables in Unity, I typed 0.5 for the firing rate the player is shooting as it its speed. I also dragged the firing point Game Object into the script along with the prefab so it will function as intended.

As a requirement to get this to work, I had to replace the sr.flipX method in favour of a float.mx method.


Next on the list of features, I’m going to add a kill plane underneath the stage so when my player touches it, he will respawn. I created a new game object and dragged it underneath, far enough to let the player freefall for a bit before it touches it.

I also added a spawn point and placed it to where my character would spawn at the start of the level.

The purpose of this script is simple. If the player collides with anything to what the script applies the tag to, the player character will be sent back to the start.

Next I wanted to apply a script that enables the bullet to instantly kill an enemy upon colliding. I jumped into the Patrol script and added a new function that will act as the collision. There is also an if statement for if the prefab does collide with the object, it will destroy it along with the bullet.
Next up, I wanted the audience to know what the controls are. To do this, I made a UI text and placed it on the canvas, naming it to what the controls are in the game. I then attached a box collider 2D to the post asset I imported into Unity and hovered it around the sign area. I also ensure the Is Trigger box is ticked to get it to work.


I went ahead and tested the function to see if the text would display upon the player colliding with the post to a good result knowing that it works.


This script essentially allows the box collider to display the UI text upon the player colliding with said collider. I also added an OnTriggerExit2D function for when the player steps away from the collider so the text wouldn’t stay on the screen regardless. I also had to attach the game objects into the script as shown below.


As you can see, it works. The UI text shows when the player moves into the collider I had set up. Now my next function would be the script to kill the player upon colliding with objects like spikes and enemies. There was one feature I had to remove in order to get this work, which I will talk more about later.

This script allows the enemy and spike objects to kill the player character once it has the box collider 2D attached to them. I went into File > Build Settings and added an open scene for the level, which by default had the number 0. I then asked the SceneManager to load the scene upon colliding with these game objects. Thankfully, I didn’t have to change anything as the number in the attached script was set to 0 by default, which was good.

All I basically had to do now was drag the script onto the enemy sprites and spikes and playtest the game to see if it works, which it does.


Leave a Reply