Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.10 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [AddComponentMenu("Camera-Control/Mouse drag Orbit with zoom")]
  5. public class DragMouseOrbit : MonoBehaviour
  6. {
  7.     public Transform target;
  8.     public float distance = 5.0f;
  9.     public float xSpeed = 120.0f;
  10.     public float ySpeed = 120.0f;
  11.    
  12.     public float yMinLimit = -20f;
  13.     public float yMaxLimit = 80f;
  14.    
  15.     public float distanceMin = .5f;
  16.     public float distanceMax = 15f;
  17.    
  18.     public float smoothTime = 2f;
  19.    
  20.     float rotationYAxis = 0.0f;
  21.     float rotationXAxis = 0.0f;
  22.    
  23.     float velocityX = 0.0f;
  24.     float velocityY = 0.0f;
  25.    
  26.     // Use this for initialization
  27.     void Start()
  28.     {
  29.         Vector3 angles = transform.eulerAngles;
  30.         rotationYAxis = angles.y;
  31.         rotationXAxis = angles.x;
  32.        
  33.         // Make the rigid body not change rotation
  34.         if (rigidbody)
  35.         {
  36.             rigidbody.freezeRotation = true;
  37.         }
  38.     }
  39.    
  40.     void LateUpdate()
  41.     {
  42.         if (target)
  43.         {
  44.             if (Input.GetMouseButton(0))
  45.             {
  46.                 velocityX += xSpeed * Input.GetAxis("Mouse X") * distance * 0.02f;
  47.                 velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
  48.             }
  49.            
  50.             rotationYAxis += velocityX;
  51.             rotationXAxis -= velocityY;
  52.            
  53.             rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
  54.  
  55.             Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
  56.             Quaternion rotation = toRotation;
  57.            
  58.             distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
  59.            
  60.             RaycastHit hit;
  61.             if (Physics.Linecast(target.position, transform.position, out hit))
  62.             {
  63.                 distance -= hit.distance;
  64.             }
  65.             Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
  66.             Vector3 position = rotation * negDistance + target.position;
  67.            
  68.             transform.rotation = rotation;
  69.             transform.position = position;
  70.            
  71.             velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
  72.             velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
  73.         }
  74.        
  75.     }
  76.    
  77.     public static float ClampAngle(float angle, float min, float max)
  78.     {
  79.         if (angle < -360F)
  80.             angle += 360F;
  81.         if (angle > 360F)
  82.             angle -= 360F;
  83.         return Mathf.Clamp(angle, min, max);
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement