Skip to content

Instantly share code, notes, and snippets.

@lordhamburger
Created October 28, 2014 04:20
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
using UnityEngine;
using System.Collections;
public class CameraShakes : MonoBehaviour
{
public bool Shaking;
private float ShakeDecay;
private float ShakeIntensity;
private Vector3 OriginalPos;
private Quaternion OriginalRot;
void OnEnable ()
{
DoShake ();
}
void Start ()
{
Shaking = false;
}
// Update is called once per frame
void Update ()
{
if (ShakeIntensity > 0) {
transform.position = OriginalPos + Random.insideUnitSphere * ShakeIntensity;
transform.rotation = new Quaternion (OriginalRot.x + Random.Range (-ShakeIntensity, ShakeIntensity) * .2f,
OriginalRot.y + Random.Range (-ShakeIntensity, ShakeIntensity) * .2f,
OriginalRot.z + Random.Range (-ShakeIntensity, ShakeIntensity) * .2f,
OriginalRot.w + Random.Range (-ShakeIntensity, ShakeIntensity) * .2f);
ShakeIntensity -= ShakeDecay;
} else if (Shaking) {
Shaking = false;
}
}
public void DoShake ()
{
OriginalPos = transform.position;
OriginalRot = transform.rotation;
ShakeIntensity = 0.3f;
ShakeDecay = 0.02f;
Shaking = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment