Yawin

orbitController

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