Isometric camera projection, rotation around player

Hey all, first and foremost; I’d like to apologise incase I’ve submitted this in the wrong thread. Secondly, I’d like to add; I’m new to scripting in C# and using Unity although I have used other languages and editors briefly. Finally; I’d like to thank you for reading this!

I’m trying to make a game with an isometric perspective. What I’m hoping to have answered is; how can a make the camera rotate around the player? I’ve followed a few tutorials here and there. Firstly I used this code for my players movement (matching the players movement with the camera angle).

(A few notes to add, ignore any code involving the “secondCamera” variable, that’s in there purely to test some other related stuff. My isometric projection is on the "firstCamera’ that has a rotation of (x=30, y=45, x=0).)

This is code attached to my player object.

    [SerializeField] float moveSpeed = 4f;
    [SerializeField] public Camera firstCamera;
    [SerializeField] public Camera secondCamera;

    Vector3 forward, right;

    void Update()
    {
        if (firstCamera.enabled == true)
        {
            forward = firstCamera.transform.forward;
            forward.y = 0;
            forward = Vector3.Normalize(forward);
            right = Quaternion.Euler(new Vector3(0, 90, 0)) * forward;
        }
        else if (secondCamera.enabled == true)
        {
            forward = secondCamera.transform.forward;
            forward.y = 0;
            forward = Vector3.Normalize(forward);
            right = Quaternion.Euler(new Vector3(0, 90, 0)) * forward;
        }
        if (Input.anyKey)
        {
            Move();
        }
    }
    void Move()
    {
        Vector3 direction = new Vector3(Input.GetAxis("HorizontalKey"), 0, Input.GetAxis("VerticalKey"));
        Vector3 rightMovement = right * moveSpeed * Time.deltaTime * Input.GetAxis("HorizontalKey");
        Vector3 upMovement = forward * moveSpeed * Time.deltaTime * Input.GetAxis("VerticalKey");

        Vector3 heading = Vector3.Normalize(rightMovement + upMovement);

        transform.forward = heading;
        transform.position += rightMovement;
        transform.position += upMovement;
    }

After using some of this code, I then wanted my camera to follow the player. The issue I found with that was, when I make the camera a child of the player; every time I moved my player in a direction, it changed the camera’s forward face. So I scoured the internet (Google) for an answer and found this code!

This code is attached to my Camera (firstCamera) object.

    [SerializeField]
    Transform target;
    [SerializeField]
    float smoothing = 5f;

    Vector3 offset;

    void Start()
    {
        offset = transform.position - target.position;
    }

    void LateUpdate()
    {
        Vector3 targetCamPos = target.position + offset;
        transform.position = Vector3.Lerp(transform.position, targetCamPos, smoothing * Time.deltaTime);
    }

Now this code corrected all the issues with following the player (without the need to child my camera object to the player). However, this code is also responsible for the next code not working correctly.

    [SerializeField] public float rotspeed;
    [SerializeField] public GameObject target;

    void Update()
    {
        transform.RotateAround(target.transform.position, Vector3.up, rotspeed * Time.deltaTime);
    }

So… With all that out of the way. I want to rotate my camera around the player as you would expect the “transform.RotateAround” to work.
Without the second section of code in this post; it works exactly how I want it to (well, for now anyway). If somebody could provide some information as to why this doesn’t work the way an “idiot” like me expects it to; that would be awesome! I could also do with someone pointing me in the right direction for a fix? I don’t necessarily want someone to code it for me; but a C# example of working code and a basic explanation of why it works would be highly appreciated too!

Camera stuff is pretty tricky… it might be best to use Cinemachine from the Unity Package Manager.

Alternately the only way to really figure out what is going on here is to instrument the code thoroughly and see what the actual values going in and out of each function are. Staring at the static code above will not reveal much.

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong: