PageRenderTime 22ms CodeModel.GetById 9ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

/Scripts/WOWCamera.js

http://acid-and-base.googlecode.com/
JavaScript | 153 lines | 102 code | 35 blank | 16 comment | 14 complexity | 20c971bbb048c2bf662e72fe8908dc99 MD5 | raw file
  1//WowCamera.js
  2
  3//When the camera hits something, it "instantly" gets behind the player. 
  4
  5//Credits to this script: "matrix211v1"
  6//found at: http://forum.unity3d.com/threads/16949-WOW-Camera-Movement/page2
  7
  8var target : Transform;
  9
 10var targetHeight = 12.0;
 11var distance = 5.0;
 12
 13var maxDistance = 20;
 14var minDistance = 2.5;
 15
 16var xSpeed = 250.0;
 17var ySpeed = 120.0;
 18
 19var yMinLimit = -20;
 20var yMaxLimit = 80;
 21
 22var zoomRate = 20;
 23
 24var rotationDampening = 3.0;
 25
 26var theta2 : float = 0.5;
 27
 28private var x = 0.0;
 29private var y = 0.0;
 30
 31private var fwd = new Vector3();
 32private var rightVector = new Vector3();
 33private var upVector = new Vector3();
 34private var movingVector = new Vector3();
 35private var collisionVector = new Vector3();
 36private var isColliding : boolean = false;
 37   
 38private var a1 = new Vector3();
 39private var b1 = new Vector3();
 40private var c1 = new Vector3();
 41private var d1 = new Vector3();
 42private var e1 = new Vector3();
 43private var f1 = new Vector3();
 44private var h1 = new Vector3();
 45private var i1 = new Vector3();
 46
 47@script AddComponentMenu("Camera-Control/WoW Camera")
 48
 49function Start () {
 50    var angles = transform.eulerAngles;
 51    x = angles.y;
 52    y = angles.x;
 53
 54   // Make the rigid body not change rotation
 55      if (rigidbody)
 56      rigidbody.freezeRotation = true;
 57}
 58
 59function LateUpdate () {
 60   if(!target)
 61      return;
 62   
 63   // If either mouse buttons are down, let them govern camera position
 64   if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
 65   {
 66   x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
 67   y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
 68   
 69   // otherwise, ease behind the target if any of the directional keys are pressed
 70   } else if(Input.GetAxis("Vertical") || Input.GetAxis("Horizontal")) {
 71      var targetRotationAngle = target.eulerAngles.y;
 72      var currentRotationAngle = transform.eulerAngles.y;
 73      x = Mathf.LerpAngle(currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);
 74   }
 75   
 76   distance -= (Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime) * zoomRate * Mathf.Abs(distance);
 77   distance = Mathf.Clamp(distance, minDistance, maxDistance);
 78   
 79   y = ClampAngle(y, yMinLimit, yMaxLimit);
 80   
 81   var rotation:Quaternion = Quaternion.Euler(y, x, 0);
 82   var position = target.position - (rotation * Vector3.forward * distance + Vector3(0,-targetHeight,0));
 83   
 84   // Check to see if we have a collision
 85   collisionVector = AdjustLineOfSight(transform.position, position);
 86   
 87   // Check Line Of Sight
 88   if (collisionVector != Vector3.zero)
 89   {
 90      Debug.Log("Check Line Of Sight");
 91       a1 = transform.position;
 92      b1 = position;
 93      c1 = AdjustLineOfSight(transform.position, position);
 94      d1 = c1 - a1;
 95      e1 = d1.normalized * -1;
 96      f1 = d1 + e1 * 1;
 97      g1 = f1 + a1;
 98      position = g1;
 99
100      // check distance player to camera
101      h1 = position - a1;
102      if (h1.magnitude < 10)
103      {
104         position = a1 - fwd * 4;
105         //position.y = targetPlayer.y;
106         theta2 = theta2 + .25;
107      }
108      
109      // set new camera distance
110      h1 = position - a1;
111      distance = h1.magnitude;
112   }
113
114   // check collision
115   if (Physics.CheckSphere (position, .5) )
116   {
117       a1 = transform.position;
118      
119      newPosition = a1 - fwd * 4;
120      //newPosition.y = targetPlayer.y;
121      theta2 = theta2 + .25;
122      
123      // set new camera distance
124      h1 = position - a1;
125      distance = h1.magnitude;
126   }   
127   
128   //position = Vector3.Slerp(transform.position, position, Time.deltaTime * 100);   
129   
130   transform.rotation = rotation;
131   transform.position = position;
132}
133
134static function ClampAngle (angle : float, min : float, max : float) {
135   if (angle < -360)
136      angle += 360;
137   if (angle > 360)
138      angle -= 360;
139   return Mathf.Clamp (angle, min, max);
140}
141
142function AdjustLineOfSight (vecA: Vector3, vecB: Vector3)
143{
144      var hit: RaycastHit;
145    
146      if (Physics.Linecast (vecA, vecB, hit))
147      {
148         Debug.Log("I hit something");
149         return hit.point;
150      }
151     
152     return Vector3.zero;
153}