Skip to content

Instantly share code, notes, and snippets.

@5outh
Created July 11, 2012 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5outh/3094373 to your computer and use it in GitHub Desktop.
Save 5outh/3094373 to your computer and use it in GitHub Desktop.
Mouse Orbit
var target : Transform;
var distance = 10.0;
var xSpeed = 250.0;
var ySpeed = 120.0;
var zoomSpeed = 10;
var yMinLimit = -20;
var yMaxLimit = 80;
var zoomMinLimit = 10;
var zoomMaxLimit = 50;
private var x = 0.0;
private var y = 0.0;
@script AddComponentMenu("Camera-Control/Mouse Orbit")
function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate () {
if (target) {
if(Input.GetMouseButton(1)){
// Only update rotation if Right Mouse button is held down
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
}
//Zooming can be done without holding down the Right Mouse button
distance -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
distance = Mathf.Clamp(distance, zoomMinLimit, zoomMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
target.rotation = rotation;
transform.rotation = rotation;
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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment