Advertisement
lemansky

Untitled

Apr 11th, 2021 (edited)
1,078
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.50 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CameraFollow : MonoBehaviour
  6. {
  7.     public GameObject player;
  8.     public Vector3 offset;
  9.  
  10.     public float rotateSpeed = 10.0f;
  11.     public float currentDistance = 0.0f;
  12.     public float cameraSpeed = 25.0f;
  13.     public float maxZoom = 3.0f;
  14.     public float minZoom = -3.0f;
  15.  
  16.     public Vector3 resetPosition;
  17.     public Quaternion resetRotation;
  18.     void Start()
  19.     {
  20.         player = GameObject.Find("Player");
  21.         offset = transform.position - player.transform.position;
  22.         resetPosition = offset;
  23.         resetRotation = transform.rotation;
  24.     }
  25.  
  26.     void LateUpdate()
  27.     {
  28.         if (Input.GetMouseButton(1))
  29.         {
  30.  
  31.             Quaternion angle = Quaternion.Euler(transform.TransformDirection(Input.GetAxisRaw("Mouse Y") * -rotateSpeed, Input.GetAxisRaw("Mouse X") * rotateSpeed, 0));            
  32.             offset = angle * offset;
  33.         }
  34.  
  35.         if (Input.GetMouseButtonDown(2))
  36.         {
  37.             offset = resetPosition;
  38.             transform.rotation = resetRotation;
  39.             currentDistance = 0.0f;
  40.         }
  41.  
  42.         currentDistance += Input.GetAxisRaw("Mouse ScrollWheel") * cameraSpeed;
  43.         currentDistance = Mathf.Clamp(currentDistance, minZoom, maxZoom);
  44.  
  45.         offset.y = Mathf.Clamp(offset.y, 0, 7);
  46.         transform.position = player.transform.position + offset + currentDistance * transform.forward;
  47.  
  48.         transform.LookAt(player.transform);
  49.     }
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement