2D Unity Lab 6 (Player Stats)
2D Unity Lab 6 (Player 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 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
• (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
• 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 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
• 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.