Skip to content

Instantly share code, notes, and snippets.

@lordhamburger
Created October 27, 2014 20:03
  • 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?
camera shake (broken)
using UnityEngine;
using System.Collections;
public class CameraShakes : MonoBehaviour {
public Transform cameraTransform;
public float shakeLength = 5;
public float shakeTimer;
public float shakeAmount = 3;
public float shakeSpeed = 20;
public bool isShaking = false;
public bool shakeOnce = false;
public Vector3 originalPos;
Vector3 newPos;
void Awake()
{
shakeTimer = shakeLength;
}
void OnEnable()
{
originalPos = cameraTransform.position;
}
void Update()
{
if (!isShaking) {
shakeOnce = true;
shakeTimer = shakeLength;
newPos = cameraTransform.position;
}
if (shakeOnce) {
Shake ();
isShaking = false;
shakeOnce = false;
}
}
public void Shake() {
if (shakeTimer > 0)
{
isShaking = true;
if (Vector3.Distance(newPos,cameraTransform.position)<=shakeAmount/30) {newPos = originalPos+Random.insideUnitSphere * shakeAmount;}
cameraTransform.position = Vector3.Lerp(cameraTransform.position, newPos , Time.deltaTime*shakeSpeed);
shakeTimer -= Time.deltaTime;
}
else
{
shakeTimer = 0f;
cameraTransform.position = originalPos;
isShaking = false;
shakeOnce = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment