Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Camera_Movement : MonoBehaviour
  5. {
  6.  
  7.     public Transform target;
  8.     public Transform targetBody;
  9.     public float targetHeight = 1.2f;
  10.     public float distance = 4.0f;
  11.     public float maxDistance = 6;
  12.     public float minDistance = 1.0f;
  13.     public float xSpeed = 250.0f;
  14.     public float ySpeed = 120.0f;
  15.     public float yMinLimit = -10;
  16.     public float yMaxLimit = 70;
  17.     public float zoomRate = 80;
  18.     public float rotationDampening = 3.0f;
  19.     private float x = 20.0f;
  20.     private float y = 0.0f;
  21.     public Quaternion aim;
  22.     public float aimAngle = 8;
  23.     public bool lockOn = false;
  24.     RaycastHit hit;
  25.     public GameObject aimStick; //For Mobile
  26.  
  27.     void Start()
  28.     {
  29.         Vector3 angles = transform.eulerAngles;
  30.         x = angles.y;
  31.         y = angles.x;
  32.  
  33.         if (GetComponent<Rigidbody>())
  34.             GetComponent<Rigidbody>().freezeRotation = true;
  35.         //Screen.lockCursor = true;
  36.         Cursor.lockState = CursorLockMode.Locked;
  37.         Cursor.visible = false;
  38.     }
  39.  
  40.     void FixedUpdate()
  41.     {
  42.         if (!target)
  43.             return;
  44.         if (!targetBody)
  45.         {
  46.             targetBody = target;
  47.         }
  48.  
  49.         if (Time.timeScale == 0.0f || GlobalCondition.freezeAll)
  50.         {
  51.             return;
  52.         }
  53.  
  54.         x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
  55.         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
  56.  
  57.  
  58.  
  59.         distance -= (Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime) * zoomRate * Mathf.Abs(distance);
  60.         distance = Mathf.Clamp(distance, minDistance, maxDistance);
  61.  
  62.         y = ClampAngle(y, yMinLimit, yMaxLimit);
  63.  
  64.         // Rotate Camera
  65.         Quaternion rotation = Quaternion.Euler(y, x, 0);
  66.         transform.rotation = rotation;
  67.         aim = Quaternion.Euler(y - aimAngle, x, 0);    
  68.  
  69.         //Camera Position
  70.  
  71.         Vector3 positiona = target.position - (rotation * Vector3.forward * distance + new Vector3(0.0f, -targetHeight, 0.0f));
  72.         transform.position = positiona;
  73.  
  74.         Vector3 trueTargetPosition = target.transform.position - new Vector3(0.0f, -targetHeight, 0.0f);
  75.  
  76.         Debug.DrawLine(trueTargetPosition, transform.position);
  77.         if (Physics.Linecast(trueTargetPosition, transform.position, out hit))
  78.         {
  79.             if (hit.transform.tag == "Wall")
  80.             {
  81.                 float tempDistance = Vector3.Distance(trueTargetPosition, hit.point) - 0.28f;
  82.  
  83.                 positiona = target.position - (rotation * Vector3.forward * tempDistance + new Vector3(0, -targetHeight, 0));
  84.                 transform.position = positiona;
  85.             }
  86.  
  87.         }
  88.     }
  89.  
  90.     static float ClampAngle(float angle, float min, float max)
  91.     {
  92.         if (angle < -360)
  93.             angle += 360;
  94.         if (angle > 360)
  95.             angle -= 360;
  96.         return Mathf.Clamp(angle, min, max);
  97.  
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement