My journey becoming a Unity game developer: Player Movement-Camera Trigger Setup

Objective: Setup the Camera Trigger game objects for the player to walk through and be detected by the colliders which will eventually make the Main Camera follow the player.

Player walks through Camera Triggers game objects.

Previously we created a path for the player to walk by clicking on the floor and having cubes created to show the spot where the player will walk to by using the Raycast to shoot a ray to the point on the floor’s collider.

Now we’re going to move the player to the actual spot on the floor without the cubes appearing. Inside the IF statement checking for the ray from the Main Camera hitting the point on the collider, remove the 2 lines of code using the cube as the player’s path.

Instead, we will create a new variable that will represent the player as a NavMeshAgent. To do this though, we must use the UnityEngine.AI for the artificial intelligence library provided by Unity Navigation system. Inside the Start() function, use the NavMeshAgent’s variable to gain access to the Player’s NavMeshAgent component which will allow us to control the player’s movement.

Then in the IF statement using the Raycast, we want to set the NavMeshAgent variable’s destination to the point where we click on the objects with colliders that have been baked for navigation for the player to walk on.

In the Scene View in the Hierarchy, we have a game object named Camera Progression Triggers. These game objects will be triggers for the Camera Progression Angles objects to follow the player as they walk through each object.

Box colliders that will serve as triggers for the cameras to follow the player.

To have these Camera Triggers to work with the Player, we will create a new C# script name CameraTrigger. Attach this script to each Camera Trigger that will need to detect the Player moving through them for the camera to follow the player.

Camera Trigger C# script attached to each Camera Progression Trigger game object.

These Camera Trigger objects have colliders on each of them that we must turn on the Is Trigger setting for the player to be detected. Also, for the Is Trigger setting to work on these objects colliders, they each will need a Rigidbody attached to them with the Use Gravity setting turned off.

Camera Triggers Is Trigger activated and rigid bodies are given to each object.

In the Camera Trigger script, we will simply use the OnTriggerEnter() function to check if the other thing that collided with the Camera Trigger object is the Player itself to print out using Debug.Log that the trigger is activated.

Finally, when we play the game we can see the player move to the spots that was clicked on and as they walk through the Camera Trigger objects, the Console will print out “Trigger activated.”

Player walking through the Camera Triggers and detected.

Next time we will actually trigger the Main Camera itself to follow the player when they walk through the Camera Triggers colliders.

Learning to become a Unity game developer.