How to Set Camera to Follow the Player in Unity | 3D Game Development

Posted January 28, 2020 by Joudet Ahsan in Game Engine, Unity

Unity and cameras are like Romeo and Juliet. When developing a game you render scenes. For example, you’re simulating a graveyard then you will need a camera to show it in the game view. You can say that camera is the eye of the player. You can set the eyes to be on a player like in First-person shooter game or behind the player like in thirst person shooter games.

This Tech-Recipe is a quick tutorial on setting up the camera o that it follows the player. There are many types of camera such as

All these types of games require different positioning of the camera. There are different types of cameras for different situations. Follow camera, dungeon crawler camera, etc. We will learn to make a follow camera today. A camera that will follow the players transform and will go along the camera

How to Set Camera to Follow the Player in Unity

There are two ways we can get a camera to follow the player. The simplest is to follow the following two steps:

1. Make sure there’s a camera in the hierarchy

2. Drag the camera, make it a child of the player.

You can do the same through C# script. You can attach the script to the camera and give it the behavior so that it follows the player.

Follow the steps below to do the same through the script

1. Click on “Add component” in the camera

2. Click on “Add a script”

3. It is better to name the script as “Follow Player”

4. Write the following script

Now the camera will follow the player. In the script what you are doing is taking the gameObject that is the player in your game (which will be followed). Then you will take another vector that will be the offset. The offset will be the distance at which you want the camera to stay from the player. Set the offset value to cameras transform minus the players transform(position). Later set the transform of the camera to players transform position plus the offset.

How to Add Rigidbody to a Gameobject in Unity | 3D Game Development

How to Detect Collision in Unity | 3D Game Development

 Can I run Unity 3d in Windows 7 64-bit?

 

 

 

About Joudet Ahsan

SEO Content Creator, a Computer Scientist in making and someone who sucks at writing bios.
View more articles by Joudet Ahsan

The Conversation

Follow the reactions below and share your own thoughts.

Leave a Reply