Advertisement
TheFuriousCoder

DragMouseOrbit

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