- Home /
Rotation not observing limits
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?
transform.Rotate(Vector3.up, 30 * Time.deltaTime);
Should also be in there. Deleted it by mistake. This line rotates an empty GameObject that acts as the target. The up and down rotations happen over the target's X-axis (target.right).
Answer by Zarack · Jan 29, 2013 at 01:49 AM
So... I solved it I guess? I'm not sure if this is optimal or efficient, but it works.
Here is my solution:
var yMinLimit = 5.0;
var yMaxLimit = 80.0;
private var lowerLimitHit = false;
private var upperLimitHit = false;
function Update () {
var current = myCamera.transform.rotation.eulerAngles;
//Has the lowerLimit or upperLimit been hit?
lowerLimitHit = current.x < yMinLimit;
upperLimitHit = current.x > yMaxLimit;
//AFTER THE BREAK....
case "cameraUp":
Debug.Log("cameraUp tapped");
if(!upperLimitHit) {
myCamera.transform.RotateAround(target.position, target.right, 15 * Time.deltaTime);
}
break;
case "cameraDown":
Debug.Log("cameraDown tapped");
if(!lowerLimitHit) {
myCamera.transform.RotateAround(target.position, target.right, -15 * Time.deltaTime);
}
break;
Your answer
![](https://koobas.hobune.stream/wayback/20220613091748im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
trying to RotateAround an object around another object using mouse 1 Answer
Edited mouselook script rotation clamp not working 0 Answers
Limit Rotation of physics object in 2D 1 Answer
Clamp Rotation Problem 1 Answer
Constrained rotation problem 2 Answers