Advertisement
EdibleCookie

mouseOrbit

Jul 25th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.44 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class mouseOrbit : 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.     public float ySpeedInverted = -120f;
  12.  
  13.     public float yMinLimit = -20f;
  14.     public float yMaxLimit = 80f;
  15.  
  16.     public float distanceMin = .5f;
  17.     public float distanceMax = 15f;
  18.  
  19.     public bool pause { get; set; }
  20.     public bool invertedMl { get; set; }
  21.  
  22.     public Vector3 angles;
  23.     public float x = 0.0f;
  24.     public float y = 0.0f;
  25.  
  26.     private Rigidbody rb;
  27.  
  28.     private void Start()
  29.     {
  30.         pause = false;
  31.         invertedMl = false;
  32.  
  33.         angles = transform.eulerAngles;
  34.         x = angles.y;
  35.         y = angles.x;
  36.  
  37.         rb = GetComponent<Rigidbody>();
  38.  
  39.         if (rb != null)
  40.         {
  41.             rb.freezeRotation = true;
  42.         }
  43.     }
  44.  
  45.     private void LateUpdate()
  46.     {
  47.         if(target != null)
  48.         {
  49.             print("Orbit has target");
  50.             if (!pause)
  51.             {
  52.                 print("Game is not paused");
  53.                 if (!invertedMl)
  54.                 {
  55.                     print("uninverted ran");
  56.                     x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
  57.                     y -= Input.GetAxis("Mouse Y") * ySpeedInverted * 0.02f;
  58.  
  59.                     y = ClampAngle(y, yMinLimit, yMaxLimit);
  60.  
  61.                     Quaternion rotation = Quaternion.Euler(y, x, 0);
  62.  
  63.                     distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
  64.  
  65.                     RaycastHit hit;
  66.                     if (Physics.Linecast(target.position, transform.position, out hit))
  67.                     {
  68.                         distance -= hit.distance;
  69.        
  70.                       }
  71.                     Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
  72.                     Vector3 position = rotation * negDistance + target.position;
  73.  
  74.                     transform.rotation = rotation;
  75.                     transform.position = position;
  76.                 }
  77.                 else
  78.                 {
  79.                     print("inverted ran");
  80.                     x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
  81.                     y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
  82.                        
  83.                     y = ClampAngle(y, yMinLimit, yMaxLimit);
  84.  
  85.                     Quaternion rotation = Quaternion.Euler(y, x, 0);
  86.  
  87.                     distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
  88.  
  89.                     RaycastHit hit;
  90.                     if (Physics.Linecast(target.position, transform.position, out hit))
  91.                     {
  92.                         distance -= hit.distance;
  93.                     }
  94.                     Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
  95.                     Vector3 position = rotation * negDistance + target.position;
  96.  
  97.                     transform.rotation = rotation;
  98.                     transform.position = position;
  99.                 }
  100.             }
  101.         }
  102.     }
  103.  
  104.     private static float ClampAngle(float angle, float min, float max)
  105.     {
  106.         if (angle < -360F)
  107.             angle += 360F;
  108.         if (angle > 360F)
  109.             angle -= 360F;
  110.         return Mathf.Clamp(angle, min, max);
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement