Advertisement
brainode

Untitled

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