Noob question about camera follow script

Hello everybody; I hope this has not been answered before, I spent all of yesterday searching the internet but couldn’t find an answer.

The situation is: I have a sphere (an IcoSphere actually) and I have a mesh which moves on the surface of the sphere, controlled by the player. The way I am doing this is to have a script which creates a parent object to the mesh; the parent object is located in the center of the sphere so player movement is achieved by rotating the parent object. Code follows for the player controller:

 void Update()
    {
        if (Input.GetKey(KeyCode.A))
        {
            transform.Rotate(0.0f, -turnSpeed * Time.deltaTime, 0.0f);
        }
        if (Input.GetKey(KeyCode.D))
        {
            transform.Rotate(0.0f, turnSpeed * Time.deltaTime, 0.0f);
        }
        else if (Input.GetKey(KeyCode.W))
        {
            Vector3 v3Axis = -Vector3.Cross(center.position - transform.position, transform.forward);
            center.rotation = Quaternion.AngleAxis(moveSpeed * Time.deltaTime, v3Axis) * center.rotation;
        }
        else if (Input.GetKey(KeyCode.S))
        {
            Vector3 v3Axis = Vector3.Cross(center.position - transform.position, transform.forward);
            center.rotation = Quaternion.AngleAxis(moveSpeed * Time.deltaTime, v3Axis) * center.rotation;
        }
    }

I would like to move the camera so that it always follows the player from behind. I can do this by making the camera a child of the player object, but since I would like to do some Lerp magic and make the camera chase with a little delay, I would like to have more control over its position as making it a child makes the position update instantaneous.

How can I have the camera position and rotation be updated so that it is always behind the player?

You have to keep in mind that when your camera is a child of the player, then all movement of its parent (the player) will affect the camera’s position also.

What I recommand:
Use Cinemachine. Makes your life pretty easy. And has all the features you need (A lot of Videos and Docs about CInemachine exists).

Alternatively if you want to make it without Cinemachine:

→ Make the camera not a child of the player
→ or use some kind of empty non-moving wrapper object for the Player and camera like:

Scene
----Wrapper
---------Player
---------Camera

→ or it gets more complicated if you want to do it as child. It is not impossible but quite annoying coding

Probably there are more ways to achieve this. The simplest (and best imo), is to just use Cinemachine or alternatively make the camera not a child of the Player object

2 Likes

Thank you for your answer, I will check cinemachine out! I do not want the camera to be a child of the player, I’d like it not to be indeed, but I am struggling with the math to keep the camera behind the player and looking at it as the player revolves around the center of the sphere.

A very simple follow cam script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MecaCamera : MonoBehaviour
{
    public Transform target;
    private Vector3 cameraOffset;
    private void Start()
    {
        cameraOffset = target.position - transform.position;
    }
    private void LateUpdate()
    {
        float angleX = target.eulerAngles.x;
        float angleY = target.eulerAngles.y;
        float angleZ = target.eulerAngles.z;

        Quaternion rotation = Quaternion.Euler(angleX, angleY, angleZ);
        transform.position = target.position - (rotation * cameraOffset);
        transform.LookAt(target);
    }
}

Thank you, I kinda of solved the problem with Cinemachine using a virtual camera with “same as follow target” as Aim property, because both the “composer” Aim property and your script have the problem that the camera turns upside down as the player revolves around the sphere; basically I’d like the camera “down” to be always pointing to the center of the sphere as it follows the player.

Have you tried to set the “Look At” Target Option (in VirtualCamera) to an empty game object in the center of the Sphere?

The flip happends for the rotation follow on the X axis. LookAat(Transform) takes a “short cut” …

With Cinemachine, you can solve the problem in the vCam properties.
Set the Body to Transposer & the Binding Mode to Lock To Target.

For the script i gave ( im not fighting with Cinemachine, its a wonderfull tool, but just to be complete… And its not MY script :stuck_out_tongue: but i use it with many variations… ) this can be fixed by adding the Vector3 WorldUp parameter to LookAt()

transform.LookAt(target, target.TransformDirection(Vector3.up));
1 Like