Advertisement
JonneOpettaja

ThirdPersonCamera.cs

Feb 11th, 2019
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.20 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class ThirdPersonCamera : MonoBehaviour
  6. {
  7.     private const float Y_ANGLE_MIN = -50.0f;
  8.     private const float Y_ANGLE_MAX = 50.0f;
  9.  
  10.     public Transform lookAt;
  11.     public Transform camTransform;
  12.  
  13.     private Camera cam;
  14.  
  15.     public float currentX = 5.0f;
  16.     public float currentY = 10.0f;
  17.     public float distance = 10.0f;
  18.     public float sensitivityX = 10.0f;
  19.     public float sensitivityY = 10.0f;
  20.  
  21.     private void Start()
  22.     {
  23.         Cursor.visible = false;
  24.         Cursor.lockState = CursorLockMode.Locked;
  25.         camTransform = transform;
  26.         cam = Camera.main;
  27.     }
  28.  
  29.     private void Update()
  30.     {
  31.         currentX += Input.GetAxis("Mouse X") * sensitivityX;
  32.         currentY += -Input.GetAxis("Mouse Y") * sensitivityY;
  33.  
  34.         currentY = Mathf.Clamp(currentY, Y_ANGLE_MIN, Y_ANGLE_MAX);
  35.     }
  36.  
  37.     private void LateUpdate()
  38.     {
  39.         Vector3 dir = new Vector3(0, 0, -distance);
  40.         Quaternion rotation = Quaternion.Euler(currentY, currentX, 0);
  41.         camTransform.position = lookAt.position + rotation * dir;
  42.         camTransform.LookAt(lookAt.position);
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement