Last active
August 5, 2016 22:10
-
-
Save chuckbergeron/9670624377164d55922c to your computer and use it in GitHub Desktop.
How I got the ProCamera2D Shake extension to work programmatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SEE THE UPDATED, PURE CODE VERSION BELOW! | |
using UnityEngine; | |
using System.Collections; | |
using Com.LuisPedroFonseca.ProCamera2D; | |
public class ProCamera2DStagingShake : MonoBehaviour { | |
private ProCamera2DShake shaker; | |
void Awake () { | |
shaker = GetComponent<ProCamera2DShake>(); | |
} | |
void Update () { | |
if (Input.GetKey("space")) { | |
shaker.Shake(); | |
} | |
} | |
} | |
// UPDATE: I've had issues with source control and Unity losing the ProCamera2D ScreenShake Presets | |
// I've defined in the inspector. So to combat that, I'm doing it all purely with code now. Here is | |
// that implementation: | |
// ScreenShakeHandler.cs, added as a component to your ProCamera camera instance | |
using Com.LuisPedroFonseca.ProCamera2D; | |
using System.Collections; | |
using UnityEngine; | |
public class ScreenShakeHandler : MonoBehaviour | |
{ | |
public enum Preset { Small, Huge, LongRumble }; | |
public static void Shake(Preset preset){ | |
if (ProCamera2DShake.Instance != null) | |
{ | |
ProCamera2DShake.Instance.StopShaking(); | |
_doShake(preset: preset); | |
} | |
} | |
private static void _doShake(Preset preset) | |
{ | |
switch (preset) | |
{ | |
case Preset.Small: | |
_doSmallShake(); | |
break; | |
case Preset.Huge: | |
_doHugeShake(); | |
break; | |
case Preset.LongRumble: | |
_doLongRumbleShake(); | |
break; | |
} | |
} | |
private static void _doSmallShake() | |
{ | |
ProCamera2DShake.Instance.Shake( | |
duration: 0.4f, | |
strength: new Vector2(10, 10), | |
vibrato: 6, | |
randomness: 0.07f, | |
smoothness: 0.1f, | |
initialAngle: -1f, | |
rotation: default(Vector3), | |
ignoreTimeScale: false | |
); | |
} | |
private static void _doHugeShake() | |
{ | |
ProCamera2DShake.Instance.Shake( | |
duration: 0.8f, | |
strength: new Vector2(20, 20), | |
vibrato: 26, | |
randomness: 0.22f, | |
smoothness: 0.2f, | |
initialAngle: -1f, | |
rotation: default(Vector3), | |
ignoreTimeScale: false | |
); | |
} | |
private static void _doLongRumbleShake() | |
{ | |
ProCamera2DShake.Instance.Shake( | |
duration: 3f, | |
strength: new Vector2(3, 3), | |
vibrato: 62, | |
randomness: 0.23f, | |
smoothness: 0.08f, | |
initialAngle: -1f, | |
rotation: default(Vector3), | |
ignoreTimeScale: false | |
); | |
} | |
} |
Glad you figured it out Chuck! ;)
This is a sign that I really need to ramp up the documentation of the plugin. I have a few things planned, but very little time!
Keep in touch and let me know if you have any other doubt.
@luispedrofonseca Just noticed your comment here. Thanks! The documentation has been pretty smooth lately, and it's been great digging in to your source code to see how things were implemented. ProCamera2D has been magic for our game.
I did notice a performance spike while profiling though. Something about ProCamera2D Shake.SetupCoroutine
. Interested to hear if you've seen that issue before?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Turns out simply using this works, too:
ProCamera2DShake.Instance.Shake();