Stuck with Camera Orbit Script

Hello
I am stuck with this orbit camera script.

It is a third person camera that stays behind an AI character (no input controls) and follows it. In the future I intent to make it follow smoothly using some steering behaviours but first i need to get the correct position of camera (So no parent to AI object also).

The objective is control the camera with three variables.

CameraDistance - Controlling the distance from target

CameraAngleX - Controlling the Horizontal offset based on backward position of AI Object (if its 0 stays in the center, when increases or decreases, it moves left or right maintain the same CameraDistance for the AI)

CameraAngleY - Controlling the Vertical offset based on backward position of AI Object (if its 0 stays in the middle, it moves top or bottom maintaining the same CameraDistance for the AI)

Here is what i came up so far

    public float CameraDistance = 5f;
    public float CameraAngleX = 10f;
    public float CameraAngleY = 30f;

    // Update is called once per frame
    void Update()
    {
        var offset = new Vector3(0, 0, -CameraDistance);

        var cameraOrbitalRotation = Quaternion.Euler(CameraAngleY, CameraAngleX, 0);

        // Achamos a posição do objeto com base na multiplicação do Quaternion pelo Vetor e adicionamos ao nosso alvo
        var cameraDesiredPosition = Target.transform.position + (cameraOrbitalRotation*offset);


        transform.position = cameraDesiredPosition;
        LookForward();
}

The Axis code actually works kind of expected but the camera is tracking only the object’s position and when AI Object rotates it doesn’t stay in back of it.

Anyone have suggestions?

EDIT 1 -

Ok, I think I’ve managed a “Workaround”, it wont work as i expected but will fit in my problem.

The code that I’ve managed was a little what @Skyway-Interactive told. but instead of using target.transform.forward in position, i used it in Offset.

Here is the actual code

void Update() {
// Use target’s forward as offset to get the correct camera distance
var offset = -Target.transform.forward * CameraDistance;

  //Find the horizontal rotation angle
  var cameraOrbitalRotation = Quaternion.Euler(0,CameraAngleX,0)*offset;

  // Apply to position and add the Vertical CameraAngleY
  var cameraDesiredPosition = Target.transform.position + (cameraOrbitalRotation) + (Target.transform.up*CameraAngleY);
  transform.position = cameraDesiredPosition;

  LookToTheTarget();
}

The only problem that I’ve saw here is the CameraAngleY won´t obey the CameraDistance, That happens because I am adding it’s position directly to the camera’s transform.
What I was intended to do is roll the Vertical Axis just like i did with the Horizontal one (CameraAngleX).

Any Suggestions?

So it looks like you need to find the “spot behind the character” not just the characters position. I would think something like: target.transform.position + (target.transform.forward *-distance) I think your code is almost there.