Advertisement
JJCUBER

Camera Controller

May 20th, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*\
  2. |                                                               |
  3. |          Made by Jason Helman © Jason Tech And Games          |
  4. |   I do not condone the use of my work without my permission   |
  5. |                                                               |
  6. \*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  7.  
  8. using System;
  9. using UnityEngine;
  10.  
  11. public class CameraController : MonoBehaviour
  12. {
  13.     public Transform player;
  14.  
  15.     // How long the object should shake for.
  16.     [NonSerialized]
  17.     public float directionalShakeDuration = 0f;
  18.  
  19.     [NonSerialized]
  20.     public float radialShakeDuration = 0f;
  21.  
  22.     // Amplitude of the shake. A larger value shakes the camera harder.
  23.     [NonSerialized]
  24.     public float directionalShakeAmount = 0.1f;
  25.  
  26.     [NonSerialized]
  27.     public float directionalDecreaseFactor = 10f;
  28.  
  29.     [NonSerialized]
  30.     public float radialShakeAmount = 0.1f;
  31.  
  32.     [NonSerialized]
  33.     public float radialDecreaseFactor = 10f;
  34.  
  35.     [NonSerialized]
  36.     public Vector2 directionalWeight = Vector2.zero;
  37.  
  38.     private Camera mainCamera;
  39.     private Transform mainCameraTransform;
  40.  
  41.     private void Awake()
  42.     {
  43.         mainCamera = Camera.main;
  44.         mainCameraTransform = mainCamera.transform;
  45.     }
  46.  
  47.     private void LateUpdate()
  48.     {
  49.         Vector3 newCameraPosition = player.position + (mainCamera.ScreenToWorldPoint(Input.mousePosition) - player.position) * 0.35f;
  50.  
  51.         if (directionalShakeDuration <= 0f)
  52.             directionalShakeDuration = 0f;
  53.         else
  54.         {
  55.             newCameraPosition += DirectionalScreenShake(directionalWeight.x, directionalWeight.y);
  56.             directionalShakeDuration -= Time.deltaTime * directionalDecreaseFactor;
  57.         }
  58.  
  59.         if (radialShakeDuration <= 0f)
  60.             radialShakeDuration = 0f;
  61.         else
  62.         {
  63.             newCameraPosition += RadialScreenShake();
  64.             radialShakeDuration -= Time.deltaTime * radialDecreaseFactor;
  65.         }
  66.  
  67.         newCameraPosition.z = -10f;
  68.  
  69.         if (mainCameraTransform.position != newCameraPosition)
  70.             mainCameraTransform.position = newCameraPosition;
  71.     }
  72.  
  73.     private Vector3 DirectionalScreenShake(float xWeight, float yWeight)
  74.     {
  75.         return UnityEngine.Random.Range(-1f, 1f) * directionalShakeAmount * xWeight * player.transform.right +
  76.                UnityEngine.Random.Range(-1f, 1f) * directionalShakeAmount * yWeight * player.transform.up;
  77.     }
  78.  
  79.     private Vector3 RadialScreenShake()
  80.     {
  81.         return UnityEngine.Random.insideUnitSphere * radialShakeAmount;
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement