- Home /
Camera Script Rolling on the Z
Hi All, I wrote this amazing script (stole it) and have modified sections (ripped it appart) to make it work as I want it to (it isn't working)
All jokes aside, I am attempting to rotate the camera around the screen with it's focus on a target. The rotation happens on a mouse drag. The rotatin is full 360. The problem is that the camera swivles on the Z axes as well as the x and y.
Please can someone help me out here. I basically take the code below, and attatch it to the Main Camera.
using UnityEngine; using System.Collections;
public class CameraControl : MonoBehaviour {
public Transform target;
public float xSpeed = 125.0f;
public float ySpeed = 50f;
public float resetTime = 1.0f; //How long to take to reset
private Vector3 startDirection = Vector3.zero; //How far away to orbit
private float x = 0.0f;
private float y = 0.0f;
private float z = 0.0f;
private Quaternion startRotation;
private Quaternion rotation;
private bool resetting = false;
private float resetTimer = 0.0f;
void LateUpdate()
{
if (Input.GetMouseButtonDown(0))
{ startDirection = (transform.position - target.position);
startRotation = transform.rotation;
x = 0.0f;
y = 0.0f;
z = 0.0f;
}
if (Input.GetMouseButton(0))
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
z = 0.0f;
Reorient();
}
}
void Reorient()
{
rotation = Quaternion.Euler(y, x, z);
transform.rotation = rotation * startRotation;
transform.position = rotation * startDirection + target.position;
}
}
Answer by Tommynator · Sep 23, 2011 at 10:47 AM
Easy - Just modify your rotation Quaternion and let it rotate only around the y-axis, by taking only your horizontal mouse movement.
rotation = Quaternion.Euler(0, x, 0);
done :)
Answer by EvanDela · Sep 25, 2011 at 11:47 PM
That works really well but it restricts me to the x-axis, in this instance I want to be able to revolve around an object by using the left and right as well as use the mouse up and down to move the camera from a side view to a top down view, the problem is that your modification restricts me moving the camera up and down now :( any other suggestions ?
Answer by EvanDela · Sep 25, 2011 at 11:47 PM
Thats great, I posted a respose, but it doesn't seem to have appeared, so I am going to try again. Firstly - thanks very much for the answer. It dows prevent the z-axis rolling - the problem is that it now has restricted my y axis movement too. When I try this...
rotation = Quaternion.Euler(x, y, 0);
The damned thing still rolls on the Z. This is just making me crazy.