How to make camera position relative to a specific target.

i’m in the process of making a camera controller component for the player, the idea is that it stays fixed on the player and can be turned left and right with the Q and E keys, and zoomed in with the mouse scroll wheel, however, the script i have set up works in every way but one, the left and right part, see, the camera rotates perfectly fine, but for whatever reason, the camera flies out a massive distance from the ball unless the ball is at 0 on both the X and Z axis:

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour
{
    public Transform target;

    public float distance;
    public float lift;
    public float XPosition;

    public bool cameraTurnIsFixed = false;

    void Update ()
    {
       transform.position = target.position + new Vector3(XPosition, lift, distance);
       transform.LookAt(target);

       if (Input.GetAxis("Mouse ScrollWheel") < 0 && distance >= -5)//Zoom out
       {
         distance -= 0.2f;
       }
       if (Input.GetAxis("Mouse ScrollWheel") > 0 && distance <= -2.5)//Zoom in
       {
         distance += 0.2f;
       }

       if (cameraTurnIsFixed == false)
       {
          if (Input.GetKey (KeyCode.Q))
          {
              transform.RotateAround(target.position, Vector3.up, 40 * Time.deltaTime);
              distance = transform.position.z;
              XPosition = transform.position.x;
          }

            if (Input.GetKey (KeyCode.E))
          {
          transform.RotateAround(target.position, -Vector3.up, 40 * Time.deltaTime);
          distance = transform.position.z;
          XPosition = transform.position.x;
          }
       }
    }
}

to fix this, i need to make the xPosition and distance variables relative to the target (the player ball), however, i don’t know how to do this to be honest, therefore i am asking here what i should do.

and please, don’t mistake this as me asking to “make my script for me”, i just need a nudge in the right direction, and in fact it would be more helpful for me because i would actually know how it works, just being handed a script with no explanation or informative comments doesn’t exactly teach me anything.

cameraTurnIsFixed is a thing i have in place for when i don’t want the player to run the camera in certain places.

the player is a rolling sphere, the camera is meant to be situated about 1.5 units above the sphere, to get a view from above to make the game feel more 3D, the distance is a short distance the camera is from the player, all the while, the player should be able to rotate the camera on the horizontal plane, almost like if it were a planet, and there was a sun around 1.5 units above the player.

monster hunter has a camera that functions like this too, though you can set the height of the camera, and the player isn’t a sphere.

so can someone assist me?, perhaps point out some documentation and where to apply it to or something.

Hey,
i figured the flaw in your code, i guess.
The update function continuously asks your camera to point at the target, whereas it wants to look someplace else while staying at (0,lift,distance), right?
Do something like this.


    if(cameraTurnIsFixed == true)
        transform.LookAt (target);
    else
    {
        if (Input.GetKey (KeyCode.Q))
        {
            transform.RotateAround(target.position, Vector3.up, 40 * Time.deltaTime);
        }
        if (Input.GetKey (KeyCode.E))
        {
            transform.RotateAround(target.position, -Vector3.up, 40 * Time.deltaTime);
        }
    }

or if you want to like orbit your camera around your player while looking at it only, not anywhere else, you could do like this:

`
    transform.position = target.position + target.up *lift - target.forward * distance;
    transform.LookAt(target);
`

the distance vector is minus, because the camera will be behind the player, if you add them it will be in front of it looking at its face.
Correct me if i am wrong, if not and this works, mark the question solved. :slight_smile: