- Home /
Help with Camera controls for custom third person controller
So here's the layout.
I have a cube. It is a nice cube. When you move the mouse left and right(X axis) it turns left and right. It rotates the entire cube on the y axis including the camera that's positioned behind it. So that's fine.
When you move the mouse up and down, however, it is not supposed to rotate the camera to look up and down. I want it to revolve around the cube just like mouseOrbit. I managed to do that by only using the code for y axis from mouseOrbit. Remember, Cube does not move here only the camera moves around it and is always pointed at the cube.
Now for the problem. The above functions for handling both the MouseX and MouseY work perfectly independently. However, if I use both at the same time, the whole thing(cube+camera) does not rotate left and right. Rather, only the cube rotates and holds the camera in place. MouseY still works perfect though.
So how do I get both MouseX and MouseY functionalities to work simultaneously? Help me and I shall shower you with upvotes and an ascii image of a kitten.
Here's the code
var target : Transform;
var cam: Camera;
var distance = 10.0;
var yMinLimit = -20;
var yMaxLimit = 80;
var sensitivityX:float = 15F;
var sensitivityY:float = 15F;
private var x = 0.0;
private var y = 0.0;
@script AddComponentMenu("Camera-Control/Mouse Orbit")
function Start () {
var angles = cam.transform.eulerAngles;
// x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate () {
if (target)
{
/*this 'ROTATES' the entire thing on the y axis
for looking left and right*/
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
/*this 'REVOLVES' the camera around the object on the X axis
for looking up and down using mouseorbit*/
y -= Input.GetAxis("Mouse Y") * sensitivityY;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, 0, 0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
cam.transform.rotation = rotation;
cam.transform.position = position;
}
}
static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}