Day 63 — Changing Camera Angles Using Triggers

Hey and welcome!

In this one I’m going to show you how to update the Main Camera’s position whenever the player reaches certain parts of the level. As you may notice from the current position we would be quite limited in where we can move our player so we need to have some camera switching in place similar to the PS1 era Resident Evil games.

Before we get to that let’s go ahead and fix that clipping issue we came across last time so that our player can intelligently navigate around the objects in our scene. Select the DetailedModel object located in Objects in the hierarchy and then check the static box in the top right and agree to change the children. Doing this let’s our Nav Agent recognise that the display cases and other objects in the scene are a part of the environment and so should not be included in the NavMesh when baking.

Speaking of, go ahead and bake your scene again and you should see now that the blue mesh is going around the new static objects:

With that in place the Nav Agent will keep the player inside the blue mesh at all times and will get the player to navigate around the display cases if it’s in their path towards the mouse click.

I don’t know about you but that’s pretty impressive!

Now with our fancy new pathing for the player it’s time to look into updating the camera view which is a very important part about 3D development that we didn’t really need to worry about with our 2D game before. Start off by creating a script called CameraTrigger and add in the following code:

public Transform MyCamera;private void OnTriggerEnter(Collider other)
if (other.tag == "Player")
Debug.Log("The player has collided with a trigger");
Camera.main.transform.position = MyCamera.transform.position;
Camera.main.transform.rotation = MyCamera.transform.rotation;

Believe it or not this is all we need code-wise. You should recognise the majority of the code here, the exception being Camera.main but all that does is reference the main camera in the scene in order to access the components and properties within. With that information we’re then assigning the positional and rotational value to that of our MyCamera object.

If you check out the image above you can see two important objects here which is Camera_Progression_Angles and Camera_Progression_Triggers. The first one holds 6 empty objects that contain the positional value that we need to assign to our main camera and the second object contains all the triggers that let our game know which camera to switch to.

As you can see there’s nothing fancy about the triggers here, they’re just stretched out cube objects that have their Mesh Renderer component turned off. Now if you remember from before when we needed to to trigger collisions there were two things we needed to make sure that were on each object, the first one being that the Is Trigger box is checked true and the second being to add a rigidbody component with the gravity checked off.

Go ahead and do that for all six of the trigger objects and add on the CameraTrigger script to each of them as well. That’s our triggers sorted out, now it’s just a case of adding in each camera into the MyCamera value to their trigger counterpart which is nice and easy since they’re all numbered correctly. Now run your game and watch the magic happen:

I’d say that’s working pretty well. The last thing we need to work on is implementing some camera follow functionality so that the camera follows our player around so that it’s easier to hit the triggers as you may have noticed that some of them are just out of reach at some angles.

You may remember from the 2D game that we had some code using LookAt in a part of it which was used for our homing missile and enemy ramming ability in order to face the direction of their intended object. We’re going to be using something similar here but luckily for us it’s not as complicated.

Create a new script along the lines of LookAtPlayer and add in the following code:

public Transform target;void Update()

That’s right, all you need is 2 lines of code if you don’t count the Update()! Now save your script and attach it to your Main Camera since that’s what we want moving when it’s looking at the player. Then drag and drop the player into the target box we defined in the script and you should get something like this:

How great is that? With just a couple of lines of code we’ve got some pretty good camera following functionality.

I had a lot of fun with this one, next time we’ll be looking at getting some animations working in the game, there’s a little bit more to it compared to our 2D animations!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Connor Fullarton

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.