using UnityEngine; using System.Collections.Generic; namespace EZCameraShake { [AddComponentMenu("EZ Camera Shake/Camera Shaker")] public class CameraShaker : MonoBehaviour { /// /// The single instance of the CameraShaker in the current scene. Do not use if you have multiple instances. /// public static CameraShaker Instance; public static Dictionary instanceList = new Dictionary(); /// /// The default position influcence of all shakes created by this shaker. /// public Vector3 DefaultPosInfluence = new Vector3(0.15f, 0.15f, 0.15f); /// /// The default rotation influcence of all shakes created by this shaker. /// public Vector3 DefaultRotInfluence = new Vector3(1, 1, 1); /// /// Offset that will be applied to the camera's default (0,0,0) rest position /// public Vector3 RestPositionOffset = new Vector3(0, 0, 0); /// /// Offset that will be applied to the camera's default (0,0,0) rest rotation /// public Vector3 RestRotationOffset = new Vector3(0, 0, 0); Vector3 posAddShake, rotAddShake; List cameraShakeInstances = new List(); void Awake() { Instance = this; instanceList.Add(gameObject.name, this); } 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(), c.PositionInfluence); rotAddShake += CameraUtilities.MultiplyVectors(c.UpdateShake(), c.RotationInfluence); } } transform.localPosition = posAddShake + RestPositionOffset; transform.localEulerAngles = rotAddShake + RestRotationOffset; } /// /// Gets the CameraShaker with the given name, if it exists. /// /// The name of the camera shaker instance. /// public static CameraShaker GetInstance(string name) { CameraShaker c; if (instanceList.TryGetValue(name, out c)) return c; Debug.LogError("CameraShake " + name + " not found!"); return null; } /// /// Starts a shake using the given preset. /// /// The preset to use. /// A CameraShakeInstance that can be used to alter the shake's properties. public CameraShakeInstance Shake(CameraShakeInstance shake) { cameraShakeInstances.Add(shake); return shake; } /// /// Shake the camera once, fading in and out over a specified durations. /// /// The intensity of the shake. /// Roughness of the shake. Lower values are smoother, higher values are more jarring. /// How long to fade in the shake, in seconds. /// How long to fade out the shake, in seconds. /// A CameraShakeInstance that can be used to alter the shake's properties. 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; } /// /// Shake the camera once, fading in and out over a specified durations. /// /// The intensity of the shake. /// Roughness of the shake. Lower values are smoother, higher values are more jarring. /// How long to fade in the shake, in seconds. /// How long to fade out the shake, in seconds. /// How much this shake influences position. /// How much this shake influences rotation. /// A CameraShakeInstance that can be used to alter the shake's properties. 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; } /// /// Start shaking the camera. /// /// The intensity of the shake. /// Roughness of the shake. Lower values are smoother, higher values are more jarring. /// How long to fade in the shake, in seconds. /// A CameraShakeInstance that can be used to alter the shake's properties. 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; } /// /// Start shaking the camera. /// /// The intensity of the shake. /// Roughness of the shake. Lower values are smoother, higher values are more jarring. /// How long to fade in the shake, in seconds. /// How much this shake influences position. /// How much this shake influences rotation. /// A CameraShakeInstance that can be used to alter the shake's properties. 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; } /// /// Gets a copy of the list of current camera shake instances. /// public List ShakeInstances { get { return new List(cameraShakeInstances); } } void OnDestroy() { instanceList.Remove(gameObject.name); } } }