My journey becoming a Unity game developer: Player Movement-Camera Trigger
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.
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.
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.
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.
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.
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.
The final results of the 6 Camera Triggers activated with the Main Camera looking at the player.