Advertisement
bluefang05

Unity camera script.

Jan 22nd, 2017
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.59 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CameraController : MonoBehaviour
  5. {
  6.     public Transform lookAt;
  7.     public Transform lookAtDead;
  8.  
  9.     public Transform camTransform;
  10.  
  11.     private Camera cam;
  12.  
  13.     public float distance = 10.0f;
  14.     public float currentX = 0.0f;
  15.     public float currentY = 0.0f;
  16.     public float maxDistance;
  17.     public float minDistance;
  18.     public float maxY;
  19.     public float minY;
  20.  
  21.     //private float sensivityX = 4.0f;
  22.     //private float sensivityY = 1.0f;
  23.  
  24.     private void Start()
  25.     {
  26.         camTransform = transform;
  27.         cam = Camera.main;
  28.     }
  29.  
  30.     private void Update()
  31.     {
  32.         if (lookAt != null)
  33.         {
  34.             currentX += Input.GetAxis("Mouse X");
  35.             currentY -= Input.GetAxis("Mouse Y");
  36.             distance += Input.GetAxis("Mouse ScrollWheel");
  37.  
  38.             if (currentY < minY)
  39.             {
  40.                 currentY = minY;
  41.             }
  42.  
  43.             if (currentY > maxY)
  44.             {
  45.                 currentY = maxY;
  46.             }
  47.  
  48.             if (distance < 2)
  49.             {
  50.                 distance = 2;
  51.             }
  52.  
  53.             if (distance > 12)
  54.             {
  55.                 distance = 12;
  56.             }
  57.  
  58.         }
  59.  
  60.     }
  61.  
  62.     private void LateUpdate()
  63.     {
  64.         if ( lookAt != null )
  65.         {
  66.             Vector3 dir = new Vector3(0, 0, -distance);
  67.             Quaternion rotation = Quaternion.Euler(currentY, currentX, 0);
  68.             camTransform.position = lookAt.position + rotation * dir;
  69.             camTransform.LookAt(lookAt.position);
  70.  
  71.         }
  72.  
  73.        
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement