We are now going to add enemies to the game. Let's do this by executing the following steps:
To add the enemy, we download and unzip the
DwarfSpriteSheet
file from the following URL:http://freeartsprites.com/roguelike/
The Dwarf sprite sheet is shown in the following image:
Name the GameObject
enemy
.Add a Box Collider by going to Add Component | Physics 2D | Box Collider 2D.
Add a Rigidbody 2D component to our enemy GameObject by going to Add Component | Physics 2D | Rigidbody 2D.
Create the enemy idle animation the same way we created the hero idle animation.
We spawn multiple enemies by creating instances of our enemy GameObject.
Add the following lines to our hero script:
var enemy:Rigidbody2D; function enemySpawn() { var enemyInstance:Rigidbody2D; enemyInstance = Instantiate(enemy, Vector3(Random.Range(2, 8),Random.Range(-4, 4),0), Quaternion.Euler(new Vector3(0,0,0))); }