Advertisement
Guest User

Untitled

a guest
Sep 11th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.53 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CameraMovement : MonoBehaviour
  5. {
  6.     public Transform Target;
  7.     public float Distance = 5.0f;
  8.     public float xSpeed = 250.0f;
  9.     public float ySpeed = 120.0f;
  10.     public float yMinLimit = -20.0f;
  11.     public float yMaxLimit = 80.0f;
  12.     public float xMinLimit = -20.0f;
  13.     public float xMaxLimit = 80.0f;
  14.  
  15.     private float x;
  16.     private float y;
  17.  
  18.     void Awake()
  19.     {
  20.         Vector3 angles = transform.eulerAngles;
  21.         x = angles.x;
  22.         y = angles.y;
  23.  
  24.         if (GetComponent<Rigidbody>() != null)
  25.         {
  26.             GetComponent<Rigidbody>().freezeRotation = true;
  27.         }
  28.     }
  29.  
  30.     void LateUpdate()
  31.     {
  32.         if (Target != null)
  33.         {
  34.             x += (float)(Input.GetAxis("Mouse X") * xSpeed * 0.02f);
  35.             y -= (float)(Input.GetAxis("Mouse Y") * ySpeed * 0.02f);
  36.  
  37.             y = ClampAngle(y, yMinLimit, yMaxLimit);
  38.             x = ClampAngle(x, xMinLimit, xMaxLimit);
  39.  
  40.             Quaternion rotation = Quaternion.Euler(y, x, 0);
  41.             Vector3 position = rotation * (new Vector3(0.0f, 4.0f, -Distance)) + Target.position;
  42.  
  43.             transform.rotation = rotation;
  44.             transform.position = position;
  45.         }
  46.     }
  47.  
  48.     private float ClampAngle(float angle, float min, float max)
  49.     {
  50.         if (angle < -360)
  51.         {
  52.             angle += 360;
  53.         }
  54.         if (angle > 360)
  55.         {
  56.             angle -= 360;
  57.         }
  58.         return Mathf.Clamp(angle, min, max);
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement