GameObject follow other GameObject also with rotation

Hi, I have a GameObject called ‘body’ and a GameObject called ‘aCamera’. What I need is when the ‘body’ moves and rotates, I need the ‘aCamera’ move and rotate along with the ‘body’. I cannot make the aCamera object a child of the ‘body’ object because I do not want the Y rotation.

The script I’ve created does the trick however it’s not that precise. So I would like to know if there is a better way to do it or maybe I need some small change. There is a offset difference between ‘body’ and ‘aCamera’, this is Vector3(0, 1.406, 0.153).

Thanks a lot already!

using System;
using Entities;
using UnityEngine;

public class PlayerBodyPositionSync : MonoBehaviour
{
    [SerializeField] private GameObject body;
    [SerializeField] private GameObject aCamera;

    private TransformClone _bodyTransformClone;
    private Vector3 _oldBodyPosition;
   
    private void Awake()
    {
        _bodyTransformClone = body.transform.Clone();

        var forward = aCamera.transform.forward;
        aCamera.transform.forward = new Vector3(forward.x, forward.y, forward.z);
    }

    private void FixedUpdate()
    {
        SyncPositionAndRotationWithVrCamera();
    }

    private void SyncPositionAndRotationWithVrCamera()
    {
        // When there is no rotation difference skip this function
        if (Math.Abs(Math.Round(aCamera.transform.localEulerAngles.y, 4) -
                     Math.Round(body.transform.localEulerAngles.y, 4)) > 0.0f)
        {
            // calculate angle between the two points
            var bodyRotation = body.transform.rotation;
            var angle = Quaternion.Angle(_bodyTransformClone.Rotation, bodyRotation);
            var isRightRotation = IsRotationToTheRight(_bodyTransformClone.Rotation, bodyRotation);
           
            _bodyTransformClone = body.transform.Clone();
           
            RotateVrCameraWithBody(isRightRotation ? angle : -angle);

            var forward = body.transform.forward;
            aCamera.transform.forward = new Vector3(forward.x, aCamera.transform.forward.y,
                forward.z);
        }

        var position = body.transform.position;
        var newPosition = position - _oldBodyPosition;
        aCamera.transform.position += new Vector3(newPosition.x, newPosition.y, newPosition.z);
        _oldBodyPosition = position;
    }

    private void RotateVrCameraWithBody(float angle)
    {
        aCamera.transform.RotateAround(body.transform.position, Vector3.up, angle);
    }

    private bool IsRotationToTheRight(Quaternion from, Quaternion to)
    {
        var fromY = from.eulerAngles.y;
        var toY = to.eulerAngles.y;
        var clockWise = 0f;
        var counterClockWise = 0f;

        if (fromY <= toY)
        {
            clockWise = toY - fromY;
            counterClockWise = fromY + (360f - toY);
        }
        else
        {
            clockWise = (360f - fromY) + toY;
            counterClockWise = fromY - toY;
        }

        return (clockWise <= counterClockWise);
    }
}

Camera controlling is (almost) always gonna be better and easier to configure with Cinemachine. Have you looked into that package?

Hi Kurt, no I did not look into that, but I will take a look at it. Thank you for the advise.

The Cinemachine is not working for me (or I can’t get it setup correctly). The following does work with an offset vector, but when I also want the camera to rotate, it will not respect the offset vector anymore. So the camera get’s behind the body object instead of 0.153F in front.

Can you tell me from a game designer standpoint the camera effect you are trying to achieve? You say you don’t want the Y rotation which is looking left to right.

No, the Y position is for looking up and down. I have a body (thirdperson ‘ethan’) I also have a camera thats positioned near the body (0, 1.403, 0.153), this is the offset from the body. The Y value 1.403 must always be respected (for jump and crouch, this will be handled later on). Also the Z value 0.153 must be respected. The camera should always be Z value from the body no matter where the body is. So when the body moves it will be cam.z = body.z + 0.153 and cam.x = body.x

However when the body rotates during walking the camera must rotate with the same angle, but it must respect the 0.153 offset always no matter what.

The effect what I’m trying to get is that the camera will follow the body by movement and rotation with a given offset from the body.