0% found this document useful (0 votes)
20 views

2D Unity Lab 6 (Player Stats)

Uploaded by

hamdyaboaouf24
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

2D Unity Lab 6 (Player Stats)

Uploaded by

hamdyaboaouf24
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Player Health & Stats |

Types of Enemies
__________________________________________

5
Prefabs
• A prefab is a master entity of a GameObject that you can use
in your game endlessly. In other words, creating a prefab is
like creating a template that contains all the characteristics of
something (e.g. Enemy), and that template can be used to
create many copies. That saves a lot of time.
To Create a Prefab
• To create a prefab, first create a folder in the Project pane under Assets
and name it Prefabs

• Select the GameObject you want to turn into a prefab from the
Hierarchy pane and drag and drop it to the prefab you created

• If it turns blue, then it has been created successfully

• If you make a change to a prefab and want it to reflect on all the copies
in the game (e.g. you made a change to Enemy #1 and you want all
instances of Enemy #1 to be updated), you must click Apply for the
changes to take effect across the entire scene
Exercise #21 – Player Stats
• Create a script that contains everything needed to control the player’s
health and lives. Call it PlayerStats and attach it to the Player gameObject

• We need the player stats to have 3 things:


• Health
• Lives
• Temporary Immunity

• Variables we’ll need are listed on the right:


Exercise #21 – Player Stats (Cont.)
• In the Start() function:

• Create a SpriteFlicker() function


Exercise #21 – Player Stats (Cont.)
• Create the TakeDamage() function
Exercise #21 – Player Stats (Cont.)
• In the Update() function:
Enemies
• You will need to add enemies to your levels to make them
more challenging and more interesting

• Your enemies can:


• Walk on the ground
• Spawn when player passes a specific point
• Fly overhead (Sin wave)
• Follow the player
Exercise #22 - Create A Super Class For Your
Enemies
• To avoid redundancy in the code, create a script and name it EnemyController
• That script will include functions and variables that can be inherited by various types
of enemies. Add those 3 variables

• (Note that since the variables are public anyway, you don’t need to give them
specific values inside the code. Do it inside Unity instead)

• Also create a Flip() function that allows your enemy to flip when it hits a wall or
another enemy. Use the code you have already written in your Player Controller code if
that’s easier for you, or use the one below:
Exercise #22 - Create A Super Class For Your
Enemies (Cont.)
• Whenever the player runs into the enemy’s collider (which has the “IsTrigger” property
turned on), the function TakeDamage() is called from another class called PlayerStats,
and the player’s health receives damage
Exercise #23 - Create a normal walking enemy
• Choose an enemy sprite to be your enemy game object

• Add a Rigidbody2D Component to it and check the Z rotation freezing box so the enemy
can’t get flipped over

• Add a box Collider 2D Component to the GameObject

• Add another Box Collider 2D Component to the GameObject and


check the “Is Trigger” box. (The trigger will be used to check for
collisions with walls or other enemies that would cause the enemy
to turn around)
Exercise #23 - Create a normal walking enemy
(Cont.)
• Create a script for your enemy gameObject and name it
WalkingEnemy.

• Create a FixedUpdate() function and write code for movement along


the x-axis
Exercise #23 - Create a normal walking enemy
(Cont.)
• Create a wall that the enemy – upon colliding with – flips and starts moving in the opposite
direction
• Inherit the OnTriggerEnter2D() function you had created earlier in the EnemyController
superclass. However, you must override it because you’ll be making some changes to the
function’s code. Compare the code below with the code from Slide 6
• Make sure to write the correct game object tag in the if statement
Exercise #23 - Create a normal walking enemy
(Cont.)
• Back in the Unity interface, don’t forget to give the enemy’s public
variables values
Exercise #24 – Create an enemy that spawns
at a certain point
• Create an empty gameObject and add a collider2D and a script file to it. Name it
SpawnPoint
• It is named so because the enemy appears when the player runs through the
gameObject’s collider!
• Set the SpawnPoint collider to “IsTrigger”
Exercise #24 – Create an enemy that spawns
at a certain point (Cont.)
• We want to make an enemy appear the moment the player character passes by a certain point.
We’ll need to use 3 scripts: SpawnedEnemy, Walking Enemy, and LevelManager

• Create an enemy prefab and keep it ready in the prefabs folder


• The script for SpawnedEnemy will only contain the OnTriggerEnter2D function() which does this:

• If the player game object passes through the collider, LevelManager is searched for and found.
The setBool function is called and given the value of true. Back in the LevelManager script, the
enemy will be spawned and will follow the player
Exercise #24 – Create an enemy that spawns
at a certain point (Cont.)
• Now go to the Level Manager and define the following variable

• The enemy is spawned from the exact location of your the Level Manager’s game
object
Exercise #24 – Create an enemy that spawns
at a certain point (Cont.)
• Finally, go back to Unity and make sure you give the Level Manager the enemy prefab
you have created. Drag it from the prefabs folder and drop it into the Enemy text box
Exercise #25 – Create an enemy that follows the
player
• Create another prefab of an enemy game object, and create a script for it called FollowerEnemy. Wherever
your player goes, this enemy follows them

• FollowerEnemy is a subclass of EnemyController

• FollowerEnemy will need a variable of type Controller (or whatever the name of your player’s script is!)

• This variable is necessary, because in the FollowerEnemy’s Start() function, the player character will be
located as seen below:

• In the Update(), MoveTowards() function is called to make the enemy follow the player even as the player’s
position changes every frame
Exercise #25 – Create an enemy that follows
the player
• A good example of a FollowerEnemy is a rotating saw like this. It can
be animated and follows the player left and right
Exercise #26 – Create a flying enemy
• Create an empty GameObject and name it “FlyingEnemy”
• Add a Sprite Renderer Component to the GameObject and point the “Sprite” property to your
enemy image
• Add a Circle Collider 2D Component to the GameObject and check the “IsTrigger” box. Add new
script to it and call it FlyingEnemy
• Note that FlyingEnemy will be a subclass of EnemyController,
so it can inherit its variables (e.g. damage) and functions
(e.g. OnTriggerEnter2D). No need to rewrite them
Exercise #26 – Create a flying enemy (Cont.)
• Declare the public variables

• Assign the temporary position to the position the enemy starts at in the scene
Exercise #26 – Create a flying enemy (Cont.)
• Draw the sin waves in the Fixed Update function

• Then add the OnTriggerEnter2D function so that if this flying


enemy touches the player, the player’s receives damage and
their health decreases
Work Tip – What are Tags?
• You can tag any game object with a keyword that the script uses in order
to execute an action (For example, the player shooting an enemy)

• We will need to ‘tag’ every creature that the player can shoot as an
‘Enemy’

• To do that:
• Select the gameObject and go to the top of the Inspector window
• Click on the Tags drop down list
• Create a new relevant tag or choose one from the list
• Assign the tag to the gameObject
Useful References
• Johnson, M., Hasankolli, R., & Henley, J. A. (2014). Learning
2D Game Development with Unity: A Hands-on Guide to Game
Creation. Pearson Education.

• Adding A Health System - Unity 2D Platformer Tutorial - Part


9. URL retrieved From:
https://www.youtube.com/watch?v=F6hUIU72JwE

• Unity RPG Tutorial 1 - Setting Up The Basics. URL retrieved


from: https://www.youtube.com/watch?v=Pk3GCgaNVTY

You might also like