Skip to content

Instantly share code, notes, and snippets.

@gluschenko
Created December 3, 2014 18:01
Show Gist options
  • Save gluschenko/97cb83533acbd7fc1930 to your computer and use it in GitHub Desktop.
Save gluschenko/97cb83533acbd7fc1930 to your computer and use it in GitHub Desktop.
var target : Transform;
var distance = 10.0;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
var x = 0.0;
var y = 0.0;
var rotation : Quaternion;
var position : Vector3;
@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 (GetComponent.<Rigidbody>())GetComponent.<Rigidbody>().freezeRotation = true;
///
UpdateRotation();
}
function LateUpdate () {
if(Input.GetKey(KeyCode.Mouse1))UpdateRotation();
transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * 8f);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * 8f);
}
function UpdateRotation()
{
if (target) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
rotation = Quaternion.Euler(y, x, 0);
position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
//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