Advertisement
lemansky

Untitled

Apr 4th, 2021
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.06 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.     public float rotateSpeed = 5.0f;
  10.     public float currentDistance = 0.0f;
  11.     public float cameraSpeed = 50.0f;
  12.     public float maxZoom = 3;
  13.     public float minZoom = -3;
  14.     void Start()
  15.     {
  16.         player = GameObject.Find("Player");
  17.         offset = transform.position - player.transform.position;
  18.     }
  19.  
  20.     void LateUpdate()
  21.     {
  22.         if (Input.GetMouseButton(1))
  23.         {
  24.             Quaternion angle = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * rotateSpeed, Vector3.up);
  25.             offset = angle * offset;
  26.  
  27.         }
  28.  
  29.         currentDistance += Input.GetAxisRaw("Mouse ScrollWheel") * cameraSpeed;
  30.         currentDistance = Mathf.Clamp(currentDistance, minZoom, maxZoom);
  31.  
  32.         transform.position = player.transform.position + offset + currentDistance * transform.forward;
  33.  
  34.         transform.LookAt(player.transform);
  35.        
  36.     }
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement