PageRenderTime 28ms CodeModel.GetById 19ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/Scripts/MouseOrbitImproved.js

http://acid-and-base.googlecode.com/
JavaScript | 62 lines | 41 code | 20 blank | 1 comment | 5 complexity | 30ae28b6cae8eb1cd80342e22aec3c9a MD5 | raw file
 1
 2var target : Transform;
 3var distance = 10.0;
 4 
 5var xSpeed = 250.0;
 6var ySpeed = 120.0;
 7 
 8var yMinLimit = -20;
 9var yMaxLimit = 80;
10 
11var distanceMin = 3;
12var distanceMax = 15;
13 
14private var x = 0.0;
15private var y = 0.0;
16 
17 
18@script AddComponentMenu("Camera-Control/Mouse Orbit")
19 
20function Start () {
21    var angles = transform.eulerAngles;
22    x = angles.y;
23    y = angles.x;
24   
25    // Make the rigid body not change rotation
26    if (rigidbody)
27        rigidbody.freezeRotation = true;
28}
29 
30function LateUpdate () {
31    if (target) {
32        x += Input.GetAxis("Mouse X") * xSpeed * distance* 0.02;
33        y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
34 
35       y = ClampAngle(y, yMinLimit, yMaxLimit);
36       
37        var rotation = Quaternion.Euler(y, x, 0);
38 
39        distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);
40       
41        var hit : RaycastHit;
42        if (Physics.Linecast (target.position, transform.position, hit)) {
43                distance -=  hit.distance;
44        }
45       
46        var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
47 
48        transform.rotation = rotation;
49        transform.position = position;
50   
51    }
52
53}
54 
55 
56static function ClampAngle (angle : float, min : float, max : float) {
57    if (angle < -360)
58        angle += 360;
59    if (angle > 360)
60        angle -= 360;
61    return Mathf.Clamp (angle, min, max);
62}