Question by
kasquer · Oct 17, 2016 at 11:53 AM ·
rotationjavascriptmouselookrotatearoundclamp
How to clamp transform.RotateAround using Javascript?
Hello, I ran into a problem while making camera script and am unsure on how to fix it myself. I am using transform.RotateAround to make the camera look up and down based on mouse movement but I don't want the camera to be able to do a full circle. How would I go about clamping this rotation?
Rotation I want to clamp:
transform.RotateAround (Ship.localPosition, transform.right, -Input.GetAxis("Mouse Y") * Time.deltaTime * 200);
Comment
Answer by JScotty · Oct 19, 2016 at 07:40 AM
I did some research and found this: http://answers.unity3d.com/questions/438836/limit-camera-rotation-with-rotatearound.html
Sot his would be the best code for you
//[SerializeField] show private variable in editor
[SerializeField] private float maxRotX = 10; // max rotation angle
[SerializeField] private float minRotX = -10; // min rotation angle
[SerializeField] private Transform target; // target (ex: ship)
private Vector3 initialVector = Vector3.forward;
void Start () {
initialVector = transform.position - target.position;
initialVector.y = 0;
}
void Update () {
float rotateDegrees = 0f;
float mouseYDelta = -Input.GetAxis("Mouse Y");
float speed = Time.deltaTime * 200;
if (mouseYDelta > 0) { // up
rotateDegrees += speed;
} else if (mouseYDelta < 0) { // down
rotateDegrees -= speed;
}
Vector3 currentVector = transform.position - target.position;
currentVector.x = 0;
float angleBetween = Vector3.Angle(initialVector, currentVector) * (Vector3.Cross(initialVector, currentVector).x > 0 ? 1 : -1); // calculating angle
float newAngle = Mathf.Clamp(angleBetween + rotateDegrees, minRotX, maxRotX); // clamping angle
rotateDegrees = newAngle - angleBetween;
transform.RotateAround(target.position, Vector3.right, rotateDegrees); // rotation
}
Hope this works out for you.
With this my camera seems to be freaking out and rotating endlessly. I checked for errors and didn't find any. Are any other things in my script breaking it? $$anonymous$$y current camera script:
#pragma strict
var Ship : Transform;
var CR : Transform;
var Distance : int;
var $$anonymous$$axZoomOut : float;
var $$anonymous$$axZoomIn : float;
//---------------------
@SerializeField
private var maxRotX : float = 10;
@SerializeField
private var $$anonymous$$RotX : float = -10;
@SerializeField
private var target : Transform;
@SerializeField
private var initialVector : Vector3 = Vector3.right;
//---------------------
function Update () {
//---------------------
var rotateDegrees : float = 1f;
var mouseYDelta : float = Input.GetAxis("$$anonymous$$ouse Y");
var speed : float = Time.deltaTime * 1;
if (mouseYDelta > 0) {
rotateDegrees += speed;
} else if (mouseYDelta) {
rotateDegrees -= speed;
}
var currentVector : Vector3 = transform.position - target.position;
currentVector.x = 0;
var angleBetween : float = Vector3.Angle(initialVector, currentVector) * (Vector3.Cross(initialVector, currentVector).x > 0 ? 1 : -1);
var newAngle : float = $$anonymous$$athf.Clamp(angleBetween + rotateDegrees, $$anonymous$$RotX, maxRotX);
rotateDegrees = newAngle - angleBetween;
transform.RotateAround(target.position, transform.right, rotateDegrees);
//---------------------
//Check Distance From Ship
Distance = Vector3.Distance(CR.position, Ship.position);
//Zoom In
if (Input.GetAxis("$$anonymous$$ouse ScrollWheel") > 0 && Distance >= $$anonymous$$axZoomIn + 1) {
CR.position += CR.forward*Time.deltaTime*20;
}
//Zoom Out
if (Input.GetAxis("$$anonymous$$ouse ScrollWheel") < 0 && Distance <= $$anonymous$$axZoomOut - 1) { // back
CR.position -= CR.forward*Time.deltaTime*20;
}
//Fix Zoom Out
if (Distance >= ($$anonymous$$axZoomOut+1)) {
CR.position += CR.forward;
}
//Fix Zoom In
if (Distance <= ($$anonymous$$axZoomIn-1)) {
CR.position -= CR.forward;
}
CR.RotateAround (Ship.localPosition, CR.up, Input.GetAxis("$$anonymous$$ouse X") * Time.deltaTime * 200);
if(!Input.Get$$anonymous$$ouseButton(2)) {
//transform.RotateAround (CR.localPosition, transform.right, -Input.GetAxis("$$anonymous$$ouse Y") * Time.deltaTime * 200);
} else if(Input.Get$$anonymous$$ouseButton(2)) {
transform.localRotation.x = 0;
transform.localRotation.y = 0;
transform.localRotation.z = 0;
}
}