Attach the main Camera to a game object

Hey Creators,

I’m wondering about the two ways to make the main camera follow the player in Unity:

  1. As mentioned in the Unity Essentials pathway, you can drag the main camera object in the Hierarchy window onto the Game Object. This makes the main camera a child of the Game Object, as indicated by the green “+” circle in the Hierarchy (as shown in the screenshot).
  2. As mentioned in the Junior Programmer pathway, you can create a MonoBehavior script and attach it to the main camera. This script can then be used to update the camera’s position to match the player’s position in this case main camera position in the hierarchy window doesn’t change.

Are these two methods functionally equivalent, or do they serve different purposes?

using UnityEngine;

public class FollowPlayer : MonoBehaviour
{

    public GameObject player;
    private Vector3 offset = new Vector3(0, 5.65f, -8.42f);
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        
    }

    // Update is called once per frame
    void LateUpdate()
    {
        // Offset Camera behind the player by adding to the player's position
        transform.position = player.transform.position + offset;
    }
}

If you make camera a child of a game object then the camera is literally anchored to that gameobject, this means if that gameobject moves the camera will move and if that gameobject rotates then camera will rotate and all this happens in a snapping manner means no smoothing.

But if you use a monobehaviour to follow your gameobject then you have full control over your camera and its movement or rotation.

Now lets take a simple example of a racing game. If you will make camera a child of your car then upon accelerating your car the camera will immediately start to follow the car giving you an effect that car has no movement but the world is just sliding backward and when you will steer your car then again camera will immediately starts to match that rotation giving the effect that car is not rotating but the world is, think of it like a video of those car enthusiasts on instagram who installs a camera on the back of their car.

But if you use monobehaviour to follow your car then you can apply smoothing too, for example upon acceleration instead of directly snapping to the position of car you will slowly try to match the speed of the car this will give an effect of speed where car will go far away from camera for a matter of seconds and then camera will slowly catch up the speed.

If in your monobehaviour you are directly putting the position of you target gameobject (Ofcourse with an offset if needed) into the camera then it will act same as making camera child of that gameobject

3 Likes

Totally clear, thanks for your response and clear example. I got it now. Appreciated. :clap:

I always have my Camera in the root. Same for practically any major component whenever possible.

Also worth noting that Unity has Cinemachine so experienced devs are practically not using the camera directly anymore since much of the challenging bits about camera handling have been solved very well with Cinemachine (eg safe and dead zones, damping, group camera, blending between cameras, autoswitching cameras based on line of sight, first/third person and orbital cameras, camera shakes, obstacle avoidance, just to name a few features).

2 Likes

They’re not equivalent because the 2nd method doesn’t rotate the camera with the player. But if the 2nd method were to also update the rotation then yes they would be equivalent.