Advertisement
kasru

Camera

Jan 9th, 2013
25,263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //****** Donations are greatly appreciated.  ******
  2. //****** You can donate directly to Jesse through paypal at  https://www.paypal.me/JEtzler   ******
  3.  
  4. var target:Transform;                           // Target to follow
  5. var targetHeight = 1.7;                         // Vertical offset adjustment
  6. var distance = 12.0;                            // Default Distance
  7. var offsetFromWall = 0.1;                       // Bring camera away from any colliding objects
  8. var maxDistance = 20;                       // Maximum zoom Distance
  9. var minDistance = 0.6;                      // Minimum zoom Distance
  10. var xSpeed = 200.0;                             // Orbit speed (Left/Right)
  11. var ySpeed = 200.0;                             // Orbit speed (Up/Down)
  12. var yMinLimit = -80;                            // Looking up limit
  13. var yMaxLimit = 80;                             // Looking down limit
  14. var zoomRate = 40;                          // Zoom Speed
  15. var rotationDampening = 3.0;                // Auto Rotation speed (higher = faster)
  16. var zoomDampening = 5.0;                    // Auto Zoom speed (Higher = faster)
  17. var collisionLayers:LayerMask = -1;     // What the camera will collide with
  18. var lockToRearOfTarget = false;             // Lock camera to rear of target
  19. var allowMouseInputX = true;                // Allow player to control camera angle on the X axis (Left/Right)
  20. var allowMouseInputY = true;                // Allow player to control camera angle on the Y axis (Up/Down)
  21.  
  22. private var xDeg = 0.0;
  23. private var yDeg = 0.0;
  24. private var currentDistance;
  25. private var desiredDistance;
  26. private var correctedDistance;
  27. private var rotateBehind = false;
  28.  
  29.  
  30. @script AddComponentMenu("Camera-Control/Third Person Camera Orbit (MMORPG Like)")
  31.  
  32. function Start ()
  33. {
  34.     var angles:Vector3 = transform.eulerAngles;
  35.     xDeg = angles.x;
  36.     yDeg = angles.y;
  37.     currentDistance = distance;
  38.     desiredDistance = distance;
  39.     correctedDistance = distance;
  40.    
  41.     // Make the rigid body not change rotation
  42.     if (rigidbody)
  43.         rigidbody.freezeRotation = true;
  44.        
  45.     if (lockToRearOfTarget)
  46.         rotateBehind = true;
  47.  }
  48.    
  49. //Only Move camera after everything else has been updated
  50. function LateUpdate ()
  51. {
  52.  
  53.  
  54.  
  55.     // Don't do anything if target is not defined
  56.     if (!target)
  57.         return;
  58.    
  59.     var vTargetOffset:Vector3;
  60.        
  61.      
  62.     // If either mouse buttons are down, let the mouse govern camera position
  63.     if (GUIUtility.hotControl == 0)
  64.     {
  65.         if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
  66.         {
  67.             //Check to see if mouse input is allowed on the axis
  68.             if (allowMouseInputX)
  69.                 xDeg += Input.GetAxis ("Mouse X") * xSpeed * 0.02;
  70.             else
  71.                 RotateBehindTarget();
  72.             if (allowMouseInputY)
  73.                 yDeg -= Input.GetAxis ("Mouse Y") * ySpeed * 0.02;
  74.            
  75.             //Interrupt rotating behind if mouse wants to control rotation
  76.             if (!lockToRearOfTarget)
  77.                 rotateBehind = false;
  78.         }
  79.        
  80.         // otherwise, ease behind the target if any of the directional keys are pressed
  81.         else if (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0 || rotateBehind)
  82.         {
  83.             //RotateBehindTarget();
  84.         }
  85.     }
  86.     yDeg = ClampAngle (yDeg, yMinLimit, yMaxLimit);
  87.  
  88.     // Set camera rotation
  89.     var rotation:Quaternion = Quaternion.Euler (yDeg, xDeg, 0);
  90.  
  91.     // Calculate the desired distance
  92.     desiredDistance -= Input.GetAxis ("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs (desiredDistance);
  93.     desiredDistance = Mathf.Clamp (desiredDistance, minDistance, maxDistance);
  94.     correctedDistance = desiredDistance;
  95.  
  96.     // Calculate desired camera position
  97.     vTargetOffset = Vector3 (0, -targetHeight, 0);
  98.     var position:Vector3 = target.position - (rotation * Vector3.forward * desiredDistance + vTargetOffset);
  99.  
  100.     // Check for collision using the true target's desired registration point as set by user using height
  101.     var collisionHit:RaycastHit;
  102.     var trueTargetPosition:Vector3 = Vector3 (target.position.x, target.position.y + targetHeight, target.position.z);
  103.  
  104.     // If there was a collision, correct the camera position and calculate the corrected distance
  105.     var isCorrected = false;
  106.     if (Physics.Linecast (trueTargetPosition, position, collisionHit, collisionLayers))
  107.     {
  108.         // Calculate the distance from the original estimated position to the collision location,
  109.         // subtracting out a safety "offset" distance from the object we hit.  The offset will help
  110.         // keep the camera from being right on top of the surface we hit, which usually shows up as
  111.         // the surface geometry getting partially clipped by the camera's front clipping plane.
  112.         correctedDistance = Vector3.Distance (trueTargetPosition, collisionHit.point) - offsetFromWall;
  113.         isCorrected = true;
  114.     }
  115.  
  116.     // For smoothing, lerp distance only if either distance wasn't corrected, or correctedDistance is more than currentDistance
  117.     currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp (currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance;
  118.  
  119.     // Keep within limits
  120.     currentDistance = Mathf.Clamp (currentDistance, minDistance, maxDistance);
  121.  
  122.     // Recalculate position based on the new currentDistance
  123.     position = target.position - (rotation * Vector3.forward * currentDistance + vTargetOffset);
  124.    
  125.     //Finally Set rotation and position of camera
  126.     transform.rotation = rotation;
  127.     transform.position = position;
  128. }
  129.  
  130. function RotateBehindTarget()
  131. {
  132.     var targetRotationAngle:float = target.eulerAngles.y;
  133.     var currentRotationAngle:float = transform.eulerAngles.y;
  134.     xDeg = Mathf.LerpAngle (currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);
  135.    
  136.     // Stop rotating behind if not completed
  137.     if (targetRotationAngle == currentRotationAngle)
  138.     {
  139.         if (!lockToRearOfTarget)
  140.             rotateBehind = false;
  141.     }
  142.     else
  143.         rotateBehind = true;
  144.  
  145. }
  146.  
  147.  
  148. static function ClampAngle (angle : float, min : float, max : float)
  149. {
  150.    if (angle < -360)
  151.       angle += 360;
  152.    if (angle > 360)
  153.       angle -= 360;
  154.    return Mathf.Clamp (angle, min, max);
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement