Advertisement
Guest User

Untitled

a guest
May 25th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
BOO 2.64 KB | None | 0 0
  1. var target : Transform;
  2. var targetHeight = 2.0;
  3. var distance = 2.8;
  4. var maxDistance = 10;
  5. var minDistance = 0.5;
  6. var xSpeed = 250.0;
  7. var ySpeed = 120.0;
  8. var yMinLimit = -40;
  9. var yMaxLimit = 80;
  10. var zoomRate = 20;
  11. var rotationDampening = 3.0;
  12. private var x = 0.0;
  13. private var y = 0.0;
  14. var isTalking:boolean = false;
  15.  
  16. @script AddComponentMenu("Camera-Control/WoW Camera")
  17.  
  18. function Start () {
  19.     var angles = transform.eulerAngles;
  20.     x = angles.y;
  21.     y = angles.x;
  22.  
  23.    // Make the rigid body not change rotation
  24.       if (rigidbody)
  25.       rigidbody.freezeRotation = true;
  26. }
  27.  
  28. function LateUpdate () {
  29.    if(!target)
  30.       return;
  31.    
  32.    // If either mouse buttons are down, let them govern camera position
  33.    if (Input.GetMouseButton(0) || (Input.GetMouseButton(1))){
  34.    x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
  35.    y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
  36.    
  37.    
  38.    // otherwise, ease behind the target if any of the directional keys are pressed
  39.    } else if(Input.GetAxis("Vertical") || Input.GetAxis("Horizontal")) {
  40.       var targetRotationAngle = target.eulerAngles.y;
  41.       var currentRotationAngle = transform.eulerAngles.y;
  42.       x = Mathf.LerpAngle(currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);
  43.     }
  44.      
  45.  
  46.    distance -= (Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime) * zoomRate * Mathf.Abs(distance);
  47.    distance = Mathf.Clamp(distance, minDistance, maxDistance);
  48.    
  49.    y = ClampAngle(y, yMinLimit, yMaxLimit);
  50.    
  51.   // ROTATE CAMERA:
  52.    var rotation:Quaternion = Quaternion.Euler(y, x, 0);
  53.    transform.rotation = rotation;
  54.    
  55.    // POSITION CAMERA:
  56.    var position = target.position - (rotation * Vector3.forward * distance + Vector3(0,-targetHeight,0));
  57.    transform.position = position;
  58.    
  59.    // IS VIEW BLOCKED?
  60.     var hit : RaycastHit;
  61.     var trueTargetPosition : Vector3 = target.transform.position - Vector3(0,-targetHeight,0);
  62.    // Cast the line to check:
  63.     if (Physics.Linecast (trueTargetPosition, transform.position, hit)) {  
  64.       // If so, shorten distance so camera is in front of object:
  65.       var tempDistance = Vector3.Distance (trueTargetPosition, hit.point) - 0.28;
  66.       // Finally, rePOSITION the CAMERA:
  67.       position = target.position - (rotation * Vector3.forward * tempDistance + Vector3(0,-targetHeight,0));
  68.       transform.position = position;
  69.    }
  70. }
  71.  
  72. static function ClampAngle (angle : float, min : float, max : float) {
  73.    if (angle < -360)
  74.       angle += 360;
  75.    if (angle > 360)
  76.       angle -= 360;
  77.    return Mathf.Clamp (angle, min, max);
  78.    
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement