Advertisement
Guest User

Untitled

a guest
May 26th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.22 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class CameraController : MonoBehaviour
  4. {
  5.     public float SensitivityX;
  6.     public float SensitivityY;
  7.  
  8.     public float MinXClamp;
  9.     public float MaxXClamp;
  10.  
  11.     public Vector3 CameraOffset;
  12.  
  13.     public float CameraOffsetLength;
  14.  
  15.     public Transform Character;
  16.     public LayerMask PlayerLayer;
  17.  
  18.     public float Smoothness;
  19.  
  20.     [HideInInspector]
  21.     public Quaternion CameraDirection;
  22.  
  23.     float x;
  24.     float y;
  25.  
  26.     private void Update()
  27.     {
  28.         x -= Input.GetAxisRaw("Mouse Y") * SensitivityX;
  29.         y += Input.GetAxisRaw("Mouse X") * SensitivityY;
  30.  
  31.         x = Mathf.Clamp(x, MinXClamp, MaxXClamp);
  32.  
  33.         CameraDirection = Quaternion.Euler(x, y, 0);
  34.  
  35.         transform.rotation = CameraDirection;
  36.  
  37.         RaycastHit hit;
  38.         if (Physics.Raycast(Character.position + CameraOffset, -transform.forward, out hit, CameraOffsetLength , PlayerLayer))
  39.         {
  40.             float c = hit.distance - CameraOffsetLength * 0.1f;
  41.             transform.position = (Character.position + CameraOffset) + -transform.forward * c;
  42.         }
  43.         else
  44.             transform.position = Character.position + CameraOffset + (-transform.forward * CameraOffsetLength);
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement