using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[AddComponentMenu("EZ Camera Shake/Camera Shaker")]
public class CameraShaker : MonoBehaviour
{
///
/// The single instance of the CameraShake in the current scene. Do not use if you have multiple instances.
///
public static CameraShaker Instance;
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);
Vector3 posAddShake, rotAddShake;
private Vector3 prePosAddShake, preRotAddShake;
List cameraShakeInstances = new List();
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;
}
///
/// 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); } }
}