From the course: Unity 5: 2D Movement in an RPG Game

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Make the camera follow the player

Make the camera follow the player - Unity Tutorial

From the course: Unity 5: 2D Movement in an RPG Game

Start my 1-month free trial

Make the camera follow the player

- [Voiceover] Now that we're able to move our player around, it's time for us to make the camera follow the player. To do this, let's go into our Scripts folder and open up the MoveCamera script. At the top of the script, we're going to create a new public field typed to GameObject called target. Then, let's scroll down to the fixed update. And at the end of our condition where we're testing to see if the camera is moving, let's type else if and look to see if the target is not equal to null. Now in order for us to move the camera to follow the player, we're simply going to set the camera's x and y position to equal the target position. To do this, we'll create a new variable called position and we'll set this to the target's transform position. And there's one thing we need to do before we change the camera's position to equal the target's position. We want to make sure that the new position always has the same z value as the current camera. To do this, we'll simply type in…

Contents