Lesson 4.2 - Follow the Player
Tutorial
·
Beginner
·
+10XP
·
60 mins
·
(949)
Unity Technologies

Overview:
The player can roll around to its heart’s content… but it has no purpose. In this lesson, we fill that purpose by creating an enemy to challenge the player! First we will give the enemy a texture of your choice, then give it the ability to bounce the player away... potentially knocking them off the cliff. Lastly, we will let the enemy chase the player around the island and spawn in random positions.
Project Outcome:
A textured and spherical enemy will spawn on the island at start, in a random location determined by a custom function. It will chase the player around the island, bouncing them off the edge if they get too close.
Languages available:
Overview Video
1. Add an enemy and a physics material
Our camera rotation and player movement are working like a charm. Next we’re going to set up an enemy and give them some special new physics to bounce the player away!
- Create a new Sphere, rename it “Enemy” reposition it, and drag a texture onto it
- Add a new RigidBody component and adjust its XYZ scale, then test
- In a new “Physics Materials” folder, Create > Physics Material, then name it “Bouncy”
- Increase the Bounciness to “1”, change Bounce Combine to “Multiply”, apply it to your player and enemy, then test
2. Create enemy script to follow player
The enemy has the power to bounce the player away, but only if the player approaches it. We must tell the enemy to follow the player’s position, chasing them around the island.
- Make a new “Enemy” script and attach it to the Enemy
- Declare 3 new variables for Rigidbody enemyRb;, GameObject player;, and public float speed;
- Initialize enemyRb = GetComponent<Rigidbody>(); and player = GameObject.Find("Player");
- In Update(), AddForce towards in the direction between the Player and the Enemy

3. Create a lookDirection variable
The enemy is now rolling towards the player, but our code is a bit messy. Let’s clean up by adding a variable for the new vector.
- In Update(), declare a new Vector3 lookDirection variable
- Set Vector3 lookDirection = (player.transform.position - transform.position).normalized;
- Implement the lookDirection variable in the AddForce call

4. Create a Spawn Manager for the enemy
Now that the enemy is acting exactly how we want, we’re going to turn it into a prefab so it can be instantiated by a Spawn Manager.
- Drag Enemy into the Prefabs folder to create a new Prefab, then delete Enemy from scene
- Create a new “Spawn Manager” object, attach a new “SpawnManager” script, and open it
- Declare a new public GameObject enemyPrefab variable then assign the prefab in the inspector
- In Start(), instantiate a new enemyPrefab at a predetermined location

5. Randomly generate spawn position
The enemy spawns at start, but it always appears in the same spot. Using the familiar Random class, we can spawn the enemy in a random position.
- In SpawnManager.cs, in Start(), create new randomly generated X and Z
- Create a new Vector3 spawnPos variable with those random X and Z positions
- Incorporate the new spawnPos variable into the Instantiate call
- Replace the hard-coded values with a spawnRange variable
- Start and Restart your project to make sure it’s working
6. Make a method return a spawn point
The code we use to generate a random spawn position is perfect, and we’re going to be using it a lot. If we want to clean the script and use this code later down the road, we should store it in a custom function.
- Create a new function Vector3 GenerateSpawnPosition() { }
- Copy and Paste the spawnPosX and spawnPosZ variables into the new method
- Add the line to return randomPos; in your new method
- Replace the code in your Instantiate call with your new function name: GenerateSpawnPosition()
7. Lesson Recap
New Functionality:
- Enemy spawns at random location on the island
- Enemy follows the player around
- Spheres bounce off of each other
New Concepts and Skills:
- Physics Materials
- Defining vectors in 3D space
- Normalizing values
- Methods with return values
Next Lesson:
- In our next lesson, we’ll create ways to fight back against these enemies using Powerups!