lemansky

Untitled

Apr 11th, 2021
660
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 = 5.0f;
  11.     public float currentDistance = 0.0f;
  12.     public float cameraSpeed = 10.0f;
  13.     public float maxZoom = 3.0f;
  14.     public float minZoom = -3.0f;
  15.  
  16.     float _verticalRotation = 0.0f;
  17.  
  18.     public Vector3 resetPosition;
  19.     public Quaternion resetrotation;
  20.     void Start()
  21.     {
  22.         player = GameObject.Find("Player");
  23.         offset = transform.position - player.transform.position;
  24.         resetPosition = offset;
  25.         resetrotation = transform.rotation;
  26.     }
  27.  
  28.     void LateUpdate()
  29.     {
  30.         if (Input.GetMouseButton(1))
  31.         {
  32.             _verticalRotation = Input.GetAxisRaw("Mouse Y") * rotateSpeed;
  33.             if (player.transform.rotation.eulerAngles.y < -90.0f || player.transform.rotation.eulerAngles.y > 90.0f)
  34.             {
  35.                 _verticalRotation = -_verticalRotation;
  36.             }
  37.             Quaternion angle = Quaternion.Euler(_verticalRotation, Input.GetAxisRaw("Mouse X") * rotateSpeed, 0);
  38.             offset = angle * offset;
  39.         }
  40.  
  41.         currentDistance += Input.GetAxisRaw("Mouse ScrollWheel") * cameraSpeed;
  42.         currentDistance = Mathf.Clamp(currentDistance, minZoom, maxZoom);
  43.  
  44.         transform.position = player.transform.position + offset + currentDistance * transform.forward;
  45.  
  46.         transform.LookAt(player.transform);
  47.  
  48.         if (Input.GetMouseButtonDown(2))
  49.         {
  50.             offset = resetPosition;
  51.             transform.rotation = resetrotation;
  52.         }
  53.     }
  54. }
  55.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×