Orbital Camera for Roll a Ball Game

OK, so I am trying to make a game sort of like the game in the Roll-A-Ball tutorial, but I need the camera to orbit. I have it rotating around the Y-axis just fine, so you can turn, but you can’t look above you or anything like that. I can get it to go along the X-axis, like it’s supposed to, with the addition of another Quaternion, but when you move the mouse diagonally, it rotates on the Z-axis too, so you’re looking at the scene sideways. I really don’t want this, is there a way I can fix it?

This is my script for a Camera Rotating function with only rotating on the Y-axis:

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    public class CameraRotate : MonoBehaviour
    {
    
        public Transform target;
        public float distance = 10.0f;
        public float sensitivity = 3.0f;
    
        private Vector3 offset;
    
        void Start()
        {
            offset = (transform.position - target.position).normalized * distance;
            transform.position = target.position + offset;
        }
    
        void Update()
        {
            Quaternion a = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * sensitivity, Vector3.up);
            offset = a * offset;
            transform.rotation = a * transform.rotation;
            transform.position = target.position + offset;
        }
    }

And that works fine. But this is what I have for making it rotate along the X-axis, but unintentionally going on the Z-axis too:

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    public class CameraRotate : MonoBehaviour
    {
    
        public Transform target;
        public float distance = 10.0f;
        public float sensitivity = 3.0f;
    
        private Vector3 offset;
    
        void Start()
        {
            offset = (transform.position - target.position).normalized * distance;
            transform.position = target.position + offset;
        }
    
        void Update()
        {
            Quaternion a = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * sensitivity, Vector3.up);
            Quaternion b = Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * sensitivity, Vector3.right);
            Quaternion q = a * b;
            offset = q * offset;
            transform.rotation = q * transform.rotation;
            transform.position = target.position + offset;
        }
    }

Quaternions are tricky like that. Try using Transform.Rotate:

Hope this helps and good luck with your game!

The rotations you’re applying using that approach aren’t using the coordinate system you think they are.

Specifically, by applying incremental modifications to a maintained Vector3-based offset, they need to be applied relative to the offset’s relative position from the target. In other words:

Quaternion b = Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * sensitivity, Vector3.right);

// Change this to...
Quaternion b = Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * sensitivity, transform.right);

With that, you’ll have a quick fix, but it doesn’t solve a much more fundamental problem when looking further ahead:

Your Y-axis needs to be limited so you can’t flip the view upside down unintentionally.

It is quite disorienting when it’s not the intended behavior…

Quaternions provide a single rotation to do what you want handled in two, which means it’s much simpler to slightly modify your approach than it is to divide the rotation into multiple others.

To do that, rather than tweaking an existing offset, you can create a new rotation and treat that as your immediate baseline for a similar, yet more-easily-customizable effect.

To give an example of this in action:

// C#
public float yMin = -20;
public float yMax = 40;
float xRotation = 0;
float yRotation = 0;
public Transform target;
public float distance = 10.0f;

void Update()
{
	Vector2 controlInput = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
	xRotation += Mathf.Repeat(controlInput.x, 360.0f);
	yRotation -= controlInput.y;
	yRotation = Mathf.Clamp(yRotation, yMin, yMax);

	Quaternion newRotation = Quaternion.AngleAxis(xRotation, Vector3.up);
	newRotation *= Quaternion.AngleAxis(yRotation, Vector3.right);
	transform.rotation = newRotation;
	transform.position = target.position - (transform.forward * distance);
}

Okay, let’s break this down now:

First, it’s not actually much different than it was before. There are just a few key changes:

1 - I define a new, absolute rotation per frame rather than keeping one going. This lets me use the xRotation and yRotation values to strictly dictate the current, absolute rotations.

2 - I clamp the yRotation value between a minimum and maximum value to limit the angle to be suitable for gameplay.

3 - I set the position manually, based on the normalized rotation Quaternion and the distance variable to pull the camera as far away as intended.


To note, xRotation is sent through Mathf.Repeat() to avoid potential floating point accuracy issues at extremely high values (in the millions of degrees it would become more noticeable, but it’s better off prevented entirely).