Diary Log 2: Creating the Functions/Additional Assets

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.

Figure 8 – Pixilart – Free online pixel art drawing tool. (Pixilart, LLC, 2021)

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.

Figure 9 – Pixilart – Free online pixel art drawing tool. (Pixilart, LLC, 2021)

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.

Figure 10 – HOW TO MAKE HEART/HEALTH SYSTEM – UNITY TUTORIAL (Blackthornprod, 2018)

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.

Figure 11 – HOW TO MAKE HEART/HEALTH SYSTEM – UNITY TUTORIAL (Blackthornprod, 2018)

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.

Figure 12 – HOW TO MAKE HEART/HEALTH SYSTEM – UNITY TUTORIAL (Blackthornprod, 2018)

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.

Figure 13 – HOW TO MAKE HEART/HEALTH SYSTEM – UNITY TUTORIAL (Blackthornprod, 2018)

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.

Figure 14 – Pixel Art Maker (Pixel Art Maker, n.d.)

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

Figure 15 – Pixel Art Maker (Pixel Art Maker, n.d.)

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!

Figure 16 – How to Make Unity 2D Enemy Patrol Movements Unity3D Tutorial (LemauDev, 2019)
Figure 17 – How to Make Unity 2D Enemy Patrol Movements Unity3D Tutorial (LemauDev, 2019)

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.

Figure 18 – 2D Camera in Unity (Cinemachine Tutorial) (Brackeys, 2018)

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.

Figure 19 – 2D Camera in Unity (Cinemachine Tutorial) (Brackeys, 2018)

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.

Figure 20 – 2D Camera in Unity (Cinemachine Tutorial) (Brackeys, 2018)

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.

Figure 21 – How To Change A Sprite From A Script In Unity (With Examples) (gamedevbeginner, 2020)

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.

Figure 22 – How To Change A Sprite From A Script In Unity (With Examples) (gamedevbeginner, 2020)
Figure 23 – How To Change A Sprite From A Script In Unity (With Examples) (gamedevbeginner, 2020)

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.

Figure 24 – How To Change A Sprite From A Script In Unity (With Examples) (gamedevbeginner, 2020)
Figure 25 – 2D Shooting | 2D Platformer in Unity #7 | 2D Game Dev Tutorial (Muddy Wolf Games, 2020)

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.

Figure 26 – 2D Shooting | 2D Platformer in Unity #7 | 2D Game Dev Tutorial (Muddy Wolf Games, 2020)

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.

Figure 27 – 2D Shooting | 2D Platformer in Unity #7 | 2D Game Dev Tutorial (Muddy Wolf Games, 2020)

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.

Figure 28 – 2D Shooting | 2D Platformer in Unity #7 | 2D Game Dev Tutorial (Muddy Wolf Games, 2020)

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.

Figure 29 – 2D Shooting | 2D Platformer in Unity #7 | 2D Game Dev Tutorial (Muddy Wolf Games, 2020)

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

Figure 30 – 2D Shooting | 2D Platformer in Unity #7 | 2D Game Dev Tutorial (Muddy Wolf Games, 2020)
Figure 31 – Player Respawn – 2D Game Development With Unity (BurgZerg Arcade, 2016)

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.

Figure 32 – Player Respawn – 2D Game Development With Unity (BurgZerg Arcade, 2016)

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

Figure 33 – Player Respawn – 2D Game Development With Unity (BurgZerg Arcade, 2016)

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.

Figure 34 – How to make enemy be killed by a bullet in Unity 2D game | OnCollisionEnter2D Unity 2D tutorial (Alexander Zotov, 2017)

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.

Figure 35 – Pixel Art Maker (Pixel Art Maker, n.d.)
Figure 36 – How to easily display text in unity by a trigger! c# (Kalm1 Games, 2018)

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.

Figure 37 – How to easily display text in unity by a trigger! c# (Kalm1 Games, 2018)
Figure 38 – How to easily display text in unity by a trigger! c# (Kalm1 Games, 2018)

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.

Figure 39 – How to easily display text in unity by a trigger! c# (Kalm1 Games, 2018)
Figure 40 – How to easily display text in unity by a trigger! c# (Kalm1 Games, 2018)

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.

Figure 41 – HOW TO KILL AND RESPAWN PLAYER-Unity Tutorial (bblakeyyy, 2019)

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.

Figure 42 – HOW TO KILL AND RESPAWN PLAYER-Unity Tutorial (bblakeyyy, 2019)

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.

Figure 43 – HOW TO KILL AND RESPAWN PLAYER-Unity Tutorial (bblakeyyy, 2019)
Figure 44 – HOW TO KILL AND RESPAWN PLAYER-Unity Tutorial (bblakeyyy, 2019

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *