Scripting the Gameplay

Protect the gate from zombies using the magical orb and saber with advanced gameplay scripting.

In this lesson, we’ll implement the game logic. The objective is to protect the gate behind the swordsman from the zombies instantiating in front of him.

Note: The gate will have health power of 100, which will be reduced by 10 points on each collision with the enemy.

We’ll stop the enemy by using the magical orb and saber.

Gate creation

To start, we’ll need to create a gate object that will serve as the protective barrier behind the swordsman. Follow these steps to create it:

  1. Create a new cube GameObject and adjust its size so that it overlaps the wall that is located behind the swordsman. Once it’s in the right position, rename the GameObject to Gate.

  2. Next, add a Box Collider component to the Gate GameObject. This will allow it to detect collisions with other objects and respond accordingly.

  3. Now, add some text to the Gate GameObject to display relevant information to the player. To do this, right-click the Gate GameObject in the “Hierarchy” window and select “UI > Text - TextMeshPro” to create a new child object. This text component can be used to display the health of the gate or any other important information that the player needs to know.

Enemy creation

For enemy creation, first, we’ll be creating an enemy tag. Import a 3D cube in the scene, create a tag from the “Tag” option, and name it “enemy.” We’ll spawn the enemy once the user grabs the magical orb. So, make sure to add the cube as a Prefab and remove it from the hierarchy. Attach the following script to the enemy Prefab.

Get hands-on with 1200+ tech skills courses.