Skip to content

Instantly share code, notes, and snippets.

@jakevsrobots
Created November 25, 2013 21:23
Show Gist options
  • Save jakevsrobots/7649147 to your computer and use it in GitHub Desktop.
Save jakevsrobots/7649147 to your computer and use it in GitHub Desktop.
A sample Unity camera shake script that works with the prefab first-person controller. Add this to the "first person controller" object.
using UnityEngine;
using System.Collections;
public class CameraShake : MonoBehaviour {
public float shakeStrength = 5;
public float shake = 1;
Vector3 originalPosition;
void Start()
{
originalPosition = transform.localPosition;
}
void LateUpdate()
{
if(Input.GetKeyDown(KeyCode.Q))
{
shake = shakeStrength;
}
Camera.main.transform.localPosition = originalPosition + (Random.insideUnitSphere * shake);
shake = Mathf.MoveTowards(shake, 0, Time.deltaTime * shakeStrength);
if(shake == 0)
{
Camera.main.transform.localPosition = originalPosition;
}
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "shake")
{
shake = shakeStrength;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment