Enemy Incoming:

Sudarshan Thube
2 min readNov 12, 2021

--

Be ready, Enemy Space ships are spotted…We are going for real war

…just kidding.

To spawn enemy battle ships we need to create prefab of enemy ship and then use same steps as as we used for laser. Only different will be that, enemy ships will spawn at interval without any interference from player like we did for fire lasers with Space key.

To create enemy space ship, do same steps as we did for player just make enemy face downward. Create separate material for enemy ship and apply it.

Enemy ship prototype.

Create new script for enemy and write code for enemy movements. create variable for direction, speed and score points and one variable to get reference of player script which will come in handy when we use contents from player script.

In start function, assign direction and player references,

“GameObject.Find” will find gameobject with name Player.

We can also use, GameObject.FindObjectOfType,GameObject.FindGameObjectsWithTag.

Here we can’t assign player object directly to enemy as we will instantiate and destroy enemy ships. so directly assign player in inspector will not work. We can also use other methods like Unity event system to get desire results, but for beginners it we will go through current method we are using.

We need to right code to move enemy ship downwards and change position of enemy ships to top when they reach bottom boundary.

To check our enemy ships working, lets place 2 enemy prefabs in hierarchy and play game.

Enemy ships movement.

As we can see enemy ships moving downward, but when ships collide with player ship nothing happens. To make it work We need to setup unity physics and colliders to detect collision between objects.

In next Article we will look in to unity physics system.

--

--