Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [AddComponentMenu("Infinite Camera-Control/Mouse Orbit with zoom")]
  5. public class MouseLookPlus : MonoBehaviour
  6. {
  7.  
  8.     public Transform target;
  9.  
  10.     public float xSpeed = 12.0f;
  11.     public float ySpeed = 12.0f;
  12.  
  13.     public float scrollSpeed = 10.0f;
  14.  
  15.     public float zoomMin = 1.0f;
  16.     public float zoomMax = 20.0f;
  17.  
  18.     public float distance = 2;
  19.     public float Ypos;
  20.  
  21.     public Vector3 position;
  22.  
  23.     public bool isActivated;
  24.  
  25.  
  26.     float x = 0.0f;
  27.     float y = 0.0f;
  28.  
  29.     // Use this for initialization
  30.     void Start()
  31.     {
  32.         Vector3 angels = transform.eulerAngles;
  33.         x = angels.y;
  34.         y = angels.x;
  35.         position = -(transform.forward * distance) + target.position;
  36.         transform.position = position;
  37.         Ypos = transform.position.y;
  38.     }
  39.  
  40.     // Update is called once per frame
  41.     void LateUpdate()
  42.     {
  43.  
  44.  
  45.  
  46.  
  47.         if (Input.GetMouseButtonDown(0))
  48.         {
  49.             isActivated = true;
  50.         }
  51.         if (Input.GetMouseButtonUp(0))
  52.         {
  53.             isActivated = false;
  54.         }
  55.  
  56.  
  57.         if (target && isActivated)
  58.         {
  59.  
  60.             x += Input.GetAxis("Mouse X") * xSpeed;
  61.             y -= Input.GetAxis("Mouse Y") * ySpeed;
  62.  
  63.  
  64.             transform.RotateAround(target.position, transform.up, x);
  65.  
  66.             transform.RotateAround(target.position, transform.right, y);
  67.  
  68.  
  69.  
  70.  
  71.             transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y, 0);
  72.             transform.rotation = Quaternion.LookRotation(target.position - transform.position);
  73.             Ypos = transform.position.y;
  74.  
  75.  
  76.             x = 0;
  77.             y = 0;
  78.         }
  79.         else
  80.         {
  81.  
  82.             if (Input.GetAxis("Mouse ScrollWheel") != 0)
  83.             {
  84.  
  85.                 distance = Vector3.Distance(transform.position, target.position);
  86.  
  87.                 distance = ZoomLimit(distance - Input.GetAxis("Mouse ScrollWheel") * scrollSpeed, zoomMin, zoomMax);
  88.  
  89.                 position = -(transform.forward * distance) + target.position;
  90.  
  91.                 Ypos = position.y;
  92.                 transform.position = position;
  93.             }
  94.         }
  95.  
  96.  
  97.         float d2 = Vector3.Distance(transform.position, target.position);
  98.         if (d2 != distance) {
  99.             position = -(transform.forward * distance) + target.position;
  100.             position.y = Ypos;
  101.             transform.position = position;
  102.  
  103.         }
  104.  
  105.     }
  106.  
  107.  
  108.     public static float ZoomLimit(float dist, float min, float max) {
  109.         if (dist < min)
  110.             dist = min;
  111.         if (dist > max)
  112.             dist = max;
  113.         return dist;
  114.     }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement