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

Rhett Haynes
3 min readOct 25, 2021

--

Objective: Activate the Main Camera to the Camera Triggers when the player walks through them. Also, have the Main Camera look at the player as they move throughout the Auction room allowing the user easier access to maneuver through the Camera Triggers.

Main Camera looking at the player as they walk through the Camera Triggers.

We want to now position the Main Camera to the Camera Progression Angles game objects located at each Camera Trigger game object. Inside of the CameraTrigger script, create a new Transform variable that will hold each Camera Progression Angle with each Camera Trigger itself.

In the OnTriggerEnter() function, IF the player collides with the Camera Triggers objects, we will move the Main Camera through each Camera Progression object by setting the Main Camera’s position and rotation to the Camera Progression Angle variable’s Transform position and rotation. This will make the Camera Trigger script modular for all of the Camera Progression Angles game objects.

Main Camera coded to move into each Camera Trigger camera’s position and rotation.

In the Hierarchy, select each Camera Trigger under the Camera Progression Triggers game object, and assign each camera under the Camera Progression Angles to that trigger game object by placing the appropriate camera into the variable’s slot under the Camera Trigger script component.

Progression cameras assigned to each Camera Trigger.

When we play the game, the player will move through each trigger and the Main Camera will align with the Progression Cameras placed. But, there’s a problem when we try to click the player to move through the 3rd trigger where we have to click at the bottom right-hand corner to make the player move through that trigger. This happens because the Main Camera can’t look at the player from a forward position, therefore allowing the user to click in an open area of the floor to move the player through the triggers properly.

Player moving through each Camera Trigger with the Main Camera following them.

To fix this issue, let’s create another new C# script name LookAtPlayer. This script will be attached to the Main Camera as we need this camera in particular to look at the player.

Script created for the Main Camera to look at the player at all times.

Inside the LookAtPlayer script, we need to create a new variable that will lock onto the Transform of the target which will be the player in this case. Under the Update() function, our code will Look At the Transform of the target which is the player itself.

In the Main Camera game object, place the Player object into the Target slot under the Look At Player script component.

Player setup as the target for the Main Camera to look at.

The final results of the 6 Camera Triggers activated with the Main Camera looking at the player.

1st Camera Trigger.
2nd Camera Trigger.
3rd Camera Trigger.
4th Camera Trigger.
5th Camera Trigger.
6th Camera Trigger.

--

--

Rhett Haynes
Rhett Haynes

Written by Rhett Haynes

Learning to become a Unity game developer.

No responses yet