I’ve been trying to get this camera controller to work all day after moving away from a mouse orbit script. Currently, four planes control the movement of the camera around an object. Left and right work with no qualms as there are no limits as to their range. I want the local rotation of the camera to go no further than 80 degrees or less than 0 or 5 (whatever works at this point). The conditions limit the rotation when going up (stops at 80.093141ish) but it completely ignores the bottom limit. Blasts right through 0 and heads straight into the 350 range.
Here is my code:
var myCamera : Camera;
var hit : RaycastHit;
var ray : Ray;
var target : Transform;
var distance = 10.0;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = 5;
var yMaxLimit = 80;
private var x = 0.0;
private var y = 0.0;
function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
myCamera.transform.rotation = rotation;
myCamera.transform.position = position;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function Update () {
var current = myCamera.transform.rotation.eulerAngles;
if(Input.touchCount == 1) {
var touch: Touch = Input.touches[0];
ray = myCamera.ScreenPointToRay(Input.touches[0].position);
if(Physics.Raycast(ray.origin, ray.direction,hit)){
switch(hit.collider.name){
case "cameraRight":
Debug.Log("cameraRight tapped");
myCamera.transform.RotateAround(target.position, Vector3.up, -30 * Time.deltaTime);
break;
case "cameraLeft":
Debug.Log("cameraLeft tapped");
myCamera.transform.RotateAround(target.position, Vector3.up, 30 * Time.deltaTime);
break;
case "cameraUp":
Debug.Log("cameraUp tapped");
if(current.x < yMaxLimit) {
myCamera.transform.RotateAround(target.position, target.right, 15 * Time.deltaTime);
}
break;
case "cameraDown":
Debug.Log("cameraDown tapped");
if(current.x > yMinLimit) {
myCamera.transform.RotateAround(target.position, target.right, -15 * Time.deltaTime);
}
break;
}
}
else if (touch.phase == TouchPhase.Ended && !Physics.Raycast(ray.origin, ray.direction,hit)) {
Debug.Log("tap cancelled");
}
}
}
What am I doing wrong?