using UnityEngine; namespace CameraShake { /// /// Contains shorthands for creating common shake types. /// public class CameraShakePresets { readonly CameraShaker shaker; public CameraShakePresets(CameraShaker shaker) { this.shaker = shaker; } /// /// Suitable for short and snappy shakes in 2D. Moves camera in X and Y axes and rotates it in Z axis. /// /// Strength of motion in X and Y axes. /// Strength of rotation in Z axis. /// Frequency of shaking. /// Number of vibrations before stop. public void ShortShake2D( float positionStrength = 0.08f, float rotationStrength = 0.1f, float freq = 25, int numBounces = 5) { BounceShake.Params pars = new BounceShake.Params { positionStrength = positionStrength, rotationStrength = rotationStrength, freq = freq, numBounces = numBounces }; shaker.RegisterShake(new BounceShake(pars)); } /// /// Suitable for longer and stronger shakes in 3D. Rotates camera in all three axes. /// /// Strength of the shake. /// Frequency of shaking. /// Number of vibrations before stop. public void ShortShake3D( float strength = 0.3f, float freq = 25, int numBounces = 5) { BounceShake.Params pars = new BounceShake.Params { axesMultiplier = new Displacement(Vector3.zero, new Vector3(1, 1, 0.4f)), rotationStrength = strength, freq = freq, numBounces = numBounces }; shaker.RegisterShake(new BounceShake(pars)); } /// /// Suitable for longer and stronger shakes in 2D. Moves camera in X and Y axes and rotates it in Z axis. /// /// Strength of motion in X and Y axes. /// Strength of rotation in Z axis. /// Duration of the shake. public void Explosion2D( float positionStrength = 1f, float rotationStrength = 3, float duration = 0.5f) { PerlinShake.NoiseMode[] modes = { new PerlinShake.NoiseMode(8, 1), new PerlinShake.NoiseMode(20, 0.3f) }; Envelope.EnvelopeParams envelopePars = new Envelope.EnvelopeParams(); envelopePars.decay = duration <= 0 ? 1 : 1 / duration; PerlinShake.Params pars = new PerlinShake.Params { strength = new Displacement(new Vector3(1, 1) * positionStrength, Vector3.forward * rotationStrength), noiseModes = modes, envelope = envelopePars, }; shaker.RegisterShake(new PerlinShake(pars)); } /// /// Suitable for longer and stronger shakes in 3D. Rotates camera in all three axes. /// /// Strength of the shake. /// Duration of the shake. public void Explosion3D( float strength = 8f, float duration = 0.7f) { PerlinShake.NoiseMode[] modes = { new PerlinShake.NoiseMode(6, 1), new PerlinShake.NoiseMode(20, 0.2f) }; Envelope.EnvelopeParams envelopePars = new Envelope.EnvelopeParams(); envelopePars.decay = duration <= 0 ? 1 : 1 / duration; PerlinShake.Params pars = new PerlinShake.Params { strength = new Displacement(Vector3.zero, new Vector3(1, 1, 0.5f) * strength), noiseModes = modes, envelope = envelopePars, }; shaker.RegisterShake(new PerlinShake(pars)); } } }