| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- [AddComponentMenu("EZ Camera Shake/Camera Shaker")]
- public class CameraShaker : MonoBehaviour
- {
- /// <summary>
- /// The single instance of the CameraShake in the current scene. Do not use if you have multiple instances.
- /// </summary>
- public static CameraShaker Instance;
- static Dictionary<string, CameraShaker> instanceList = new Dictionary<string,CameraShaker>();
- /// <summary>
- /// The default position influcence of all shakes created by this shaker.
- /// </summary>
- public Vector3 DefaultPosInfluence = new Vector3(0.15f, 0.15f, 0.15f);
- /// <summary>
- /// The default rotation influcence of all shakes created by this shaker.
- /// </summary>
- public Vector3 DefaultRotInfluence = new Vector3(1, 1, 1);
- Vector3 posAddShake, rotAddShake;
- private Vector3 prePosAddShake, preRotAddShake;
- List<CameraShakeInstance> cameraShakeInstances = new List<CameraShakeInstance>();
- void Awake()
- {
- Instance = this;
- if(!instanceList.ContainsKey(gameObject.name))
- instanceList.Add(gameObject.name, this);
- }
- void Start()
- {
- prePosAddShake = Vector3.zero;
- preRotAddShake = Vector3.zero;
- }
- void Update()
- {
- posAddShake = Vector3.zero;
- rotAddShake = Vector3.zero;
- for (int i = 0; i < cameraShakeInstances.Count; i++)
- {
- if (i >= cameraShakeInstances.Count)
- break;
- CameraShakeInstance c = cameraShakeInstances[i];
- if (c.CurrentState == CameraShakeState.Inactive && c.DeleteOnInactive)
- {
- cameraShakeInstances.RemoveAt(i);
- i--;
- }
- else if (c.CurrentState != CameraShakeState.Inactive)
- {
- posAddShake += CameraUtilities.MultiplyVectors(c.UpdateShake(Time.deltaTime), c.PositionInfluence);
- rotAddShake += CameraUtilities.MultiplyVectors(c.UpdateShake(Time.deltaTime), c.RotationInfluence);
- }
- }
-
- transform.localPosition += posAddShake - prePosAddShake;
- transform.localEulerAngles += rotAddShake - preRotAddShake;
- prePosAddShake = posAddShake;
- preRotAddShake = rotAddShake;
- }
- /// <summary>
- /// Gets the CameraShaker with the given name, if it exists.
- /// </summary>
- /// <param name="name">The name of the camera shaker instance.</param>
- /// <returns></returns>
- public static CameraShaker GetInstance(string name)
- {
- CameraShaker c;
- if (instanceList.TryGetValue(name, out c))
- return c;
- Debug.LogError("CameraShake " + name + " not found!");
- return null;
- }
- /// <summary>
- /// Starts a shake using the given preset.
- /// </summary>
- /// <param name="shake">The preset to use.</param>
- /// <returns>A CameraShakeInstance that can be used to alter the shake's properties.</returns>
- public CameraShakeInstance Shake(CameraShakeInstance shake)
- {
- cameraShakeInstances.Add(shake);
- return shake;
- }
- /// <summary>
- /// Shake the camera once, fading in and out over a specified durations.
- /// </summary>
- /// <param name="magnitude">The intensity of the shake.</param>
- /// <param name="roughness">Roughness of the shake. Lower values are smoother, higher values are more jarring.</param>
- /// <param name="fadeInTime">How long to fade in the shake, in seconds.</param>
- /// <param name="fadeOutTime">How long to fade out the shake, in seconds.</param>
- /// <returns>A CameraShakeInstance that can be used to alter the shake's properties.</returns>
- public CameraShakeInstance ShakeOnce(float magnitude, float roughness, float fadeInTime, float fadeOutTime)
- {
- CameraShakeInstance shake = new CameraShakeInstance(magnitude, roughness, fadeInTime, fadeOutTime);
- shake.PositionInfluence = DefaultPosInfluence;
- shake.RotationInfluence = DefaultRotInfluence;
- cameraShakeInstances.Add(shake);
- return shake;
- }
- /// <summary>
- /// Shake the camera once, fading in and out over a specified durations.
- /// </summary>
- /// <param name="magnitude">The intensity of the shake.</param>
- /// <param name="roughness">Roughness of the shake. Lower values are smoother, higher values are more jarring.</param>
- /// <param name="fadeInTime">How long to fade in the shake, in seconds.</param>
- /// <param name="fadeOutTime">How long to fade out the shake, in seconds.</param>
- /// <param name="posInfluence">How much this shake influences position.</param>
- /// <param name="rotInfluence">How much this shake influences rotation.</param>
- /// <returns>A CameraShakeInstance that can be used to alter the shake's properties.</returns>
- public CameraShakeInstance ShakeOnce(float magnitude, float roughness, float fadeInTime, float fadeOutTime, Vector3 posInfluence, Vector3 rotInfluence)
- {
- CameraShakeInstance shake = new CameraShakeInstance(magnitude, roughness, fadeInTime, fadeOutTime);
- shake.PositionInfluence = posInfluence;
- shake.RotationInfluence = rotInfluence;
- cameraShakeInstances.Add(shake);
- return shake;
- }
- /// <summary>
- /// Start shaking the camera.
- /// </summary>
- /// <param name="magnitude">The intensity of the shake.</param>
- /// <param name="roughness">Roughness of the shake. Lower values are smoother, higher values are more jarring.</param>
- /// <param name="fadeInTime">How long to fade in the shake, in seconds.</param>
- /// <returns>A CameraShakeInstance that can be used to alter the shake's properties.</returns>
- public CameraShakeInstance StartShake(float magnitude, float roughness, float fadeInTime)
- {
- CameraShakeInstance shake = new CameraShakeInstance(magnitude, roughness);
- shake.PositionInfluence = DefaultPosInfluence;
- shake.RotationInfluence = DefaultRotInfluence;
- shake.StartFadeIn(fadeInTime);
- cameraShakeInstances.Add(shake);
- return shake;
- }
- /// <summary>
- /// Start shaking the camera.
- /// </summary>
- /// <param name="magnitude">The intensity of the shake.</param>
- /// <param name="roughness">Roughness of the shake. Lower values are smoother, higher values are more jarring.</param>
- /// <param name="fadeInTime">How long to fade in the shake, in seconds.</param>
- /// <param name="posInfluence">How much this shake influences position.</param>
- /// <param name="rotInfluence">How much this shake influences rotation.</param>
- /// <returns>A CameraShakeInstance that can be used to alter the shake's properties.</returns>
- public CameraShakeInstance StartShake(float magnitude, float roughness, float fadeInTime, Vector3 posInfluence, Vector3 rotInfluence)
- {
- CameraShakeInstance shake = new CameraShakeInstance(magnitude, roughness);
- shake.PositionInfluence = posInfluence;
- shake.RotationInfluence = rotInfluence;
- shake.StartFadeIn(fadeInTime);
- cameraShakeInstances.Add(shake);
- return shake;
- }
- /// <summary>
- /// Gets a copy of the list of current camera shake instances.
- /// </summary>
- public List<CameraShakeInstance> ShakeInstances
- { get { return new List<CameraShakeInstance>(cameraShakeInstances); } }
- }
|