Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*\
- | |
- | Made by Jason Helman © Jason Tech And Games |
- | I do not condone the use of my work without my permission |
- | |
- \*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
- using System;
- using UnityEngine;
- public class CameraController : MonoBehaviour
- {
- public Transform player;
- // How long the object should shake for.
- [NonSerialized]
- public float directionalShakeDuration = 0f;
- [NonSerialized]
- public float radialShakeDuration = 0f;
- // Amplitude of the shake. A larger value shakes the camera harder.
- [NonSerialized]
- public float directionalShakeAmount = 0.1f;
- [NonSerialized]
- public float directionalDecreaseFactor = 10f;
- [NonSerialized]
- public float radialShakeAmount = 0.1f;
- [NonSerialized]
- public float radialDecreaseFactor = 10f;
- [NonSerialized]
- public Vector2 directionalWeight = Vector2.zero;
- private Camera mainCamera;
- private Transform mainCameraTransform;
- private void Awake()
- {
- mainCamera = Camera.main;
- mainCameraTransform = mainCamera.transform;
- }
- private void LateUpdate()
- {
- Vector3 newCameraPosition = player.position + (mainCamera.ScreenToWorldPoint(Input.mousePosition) - player.position) * 0.35f;
- if (directionalShakeDuration <= 0f)
- directionalShakeDuration = 0f;
- else
- {
- newCameraPosition += DirectionalScreenShake(directionalWeight.x, directionalWeight.y);
- directionalShakeDuration -= Time.deltaTime * directionalDecreaseFactor;
- }
- if (radialShakeDuration <= 0f)
- radialShakeDuration = 0f;
- else
- {
- newCameraPosition += RadialScreenShake();
- radialShakeDuration -= Time.deltaTime * radialDecreaseFactor;
- }
- newCameraPosition.z = -10f;
- if (mainCameraTransform.position != newCameraPosition)
- mainCameraTransform.position = newCameraPosition;
- }
- private Vector3 DirectionalScreenShake(float xWeight, float yWeight)
- {
- return UnityEngine.Random.Range(-1f, 1f) * directionalShakeAmount * xWeight * player.transform.right +
- UnityEngine.Random.Range(-1f, 1f) * directionalShakeAmount * yWeight * player.transform.up;
- }
- private Vector3 RadialScreenShake()
- {
- return UnityEngine.Random.insideUnitSphere * radialShakeAmount;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement