Making the camera follow an Object in Unity3D

Today we are going to implement some camera settings for our game. We want the camera to follow the player.

There are many possibilities to achieve something like this, so we gonna try some of them.

The simplest approach of them all is to set the camera object behind our player object and make our object the parent.

This way we don’t need to code anything and we get a very fast working camera in our game.

And it looks in our project scene something like this:

And in Playmode:

It’s not that bad, but I guess we can do it better.

Let’s start writing some scripts:

using UnityEngine;
public class CameraFollow : MonoBehaviour
{
    public Transform playerObject;
    void Update()
    {
        Vector3 lookOnObject = playerObject.position - transform.position;
        lookOnObject = playerObject.position - transform.position;
        transform.forward = lookOnObject.normalized;
    }
}

Here is the script for “looking” at the player. It doesn’t follow the player yet, but it’s a nice start.

Other than that, don’t forget to separate the camera object and player object! And don’t forget to add the player object to our script.

Now we need to write script which will follow our player:

using UnityEngine;
public class CameraFollow : MonoBehaviour
{
    public Transform playerObject;

    public float distanceFromObject = 6f;
    void Update()
    {
        Vector3 lookOnObject = playerObject.position - transform.position;
        lookOnObject = playerObject.position - transform.position;
        transform.forward = lookOnObject.normalized;

        Vector3 playerLastPosition;
        playerLastPosition = playerObject.position - lookOnObject.normalized * distanceFromObject;
        transform.position = playerLastPosition;    
    }
}

We take our player Object last position, subtract it with normalized Vector of lookOnObject variable and multiply with the distance in which the camera should follow our player.

It looks complicated on paper. But that’s just the simple formula:

playerLastPosition = playerObject.position - lookOnObject.normalized * distanceFromObject;

And we get something like this:

Almost everything works fine. The camera is following the player, but… it is a little low, so we gonna need to do something with it.

using UnityEngine;
public class CameraFollow : MonoBehaviour
{
    public Transform playerObject;

    public float distanceFromObject = 6f;
    void Update()
    {
        Vector3 lookOnObject = playerObject.position - transform.position;
        lookOnObject = playerObject.position - transform.position;
        transform.forward = lookOnObject.normalized;

        Vector3 playerLastPosition;
        playerLastPosition = playerObject.position - lookOnObject.normalized * distanceFromObject;

        playerLastPosition.y = playerObject.position.y + distanceFromObject/2;
        transform.position = playerLastPosition;    
    }
}

We get the “y” transform position of the playerObject position and sum it with the divided distanceFromObject through 2. This way we get the perfect view.

And here is an example when we change the public variable from 5 to 8:

That’s all for today. I hope you learned something from this tutorial and the scripts helped you with your project.

Other than that, check other tutorials in the Unity3D series:

How to turn the Bulb light On with Script in Unity3D

Basic movement in Unity3D

 

How to make the player Object Jump in Unity 3D.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.