Created
November 25, 2013 21:23
-
-
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.
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
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