PageRenderTime 32ms CodeModel.GetById 23ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/Scripts/MouseOrbitDrag.js

http://acid-and-base.googlecode.com/
JavaScript | 59 lines | 43 code | 13 blank | 3 comment | 8 complexity | 257d9e35cbeb9315536636b0b4cd1ef1 MD5 | raw file
 1var target : Transform;
 2var distance = 10.0;
 3var cameraSpeed = 5;
 4
 5var xSpeed = 175.0;
 6var ySpeed = 75.0;
 7
 8var yMinLimit = 20; //Lowest vertical angle in respect with the target.
 9var yMaxLimit = 80;
10
11var minDistance = 5; //Min distance of the camera from the target
12var maxDistance = 20;
13
14private var x = 0.0;
15private var y = 0.0;
16
17function Start () {
18    var angles = transform.eulerAngles;
19    x = angles.y;
20    y = angles.x;
21
22   // Make the rigid body not change rotation
23      if (rigidbody)
24      rigidbody.freezeRotation = true;
25}
26
27function LateUpdate () {
28	if(Time.timeScale != 0)
29	{
30		if (target && camera) {
31
32	   //Zooming with mouse
33	   distance -= Input.GetAxis("Mouse ScrollWheel")*distance;
34	   distance = Mathf.Clamp(distance, minDistance, maxDistance);
35
36	   //Detect mouse drag;
37	   if(Input.GetMouseButton(0))   {
38	   
39			x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
40			y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;       
41		  }
42		  y = ClampAngle(y, yMinLimit, yMaxLimit);
43				 
44			var rotation = Quaternion.Euler(y, x, 0);
45			var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
46			 
47	   transform.position = Vector3.Lerp (transform.position, position, cameraSpeed*Time.deltaTime);
48		  transform.rotation = rotation;     
49		}
50	}
51}
52
53static function ClampAngle (angle : float, min : float, max : float) {
54   if (angle < -360)
55      angle += 360;
56   if (angle > 360)
57      angle -= 360;
58   return Mathf.Clamp (angle, min, max);
59}