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;
}
}