using UnityEngine; | |
using System.Collections; | |
public class CameraShake : MonoBehaviour | |
{ | |
// Transform of the camera to shake. Grabs the gameObject's transform | |
// if null. | |
public Transform camTransform; | |
// How long the object should shake for. | |
public float shakeDuration = 0f; | |
// Amplitude of the shake. A larger value shakes the camera harder. | |
public float shakeAmount = 0.7f; | |
public float decreaseFactor = 1.0f; | |
Vector3 originalPos; | |
void Awake() | |
{ | |
if (camTransform == null) | |
{ | |
camTransform = GetComponent(typeof(Transform)) as Transform; | |
} | |
} | |
void OnEnable() | |
{ | |
originalPos = camTransform.localPosition; | |
} | |
void Update() | |
{ | |
if (shakeDuration > 0) | |
{ | |
camTransform.localPosition = originalPos + Random.insideUnitSphere * shakeAmount; | |
shakeDuration -= Time.deltaTime * decreaseFactor; | |
} | |
else | |
{ | |
shakeDuration = 0f; | |
camTransform.localPosition = originalPos; | |
} | |
} | |
} |
Thanks @ixikos. I expanded on your script to calculate a delta time so the shake duration is retained even when the game gets paused (and Time.deltaTime stops).
Couldn't you just have replaced Time.deltaTime with Time.unscaledDeltaTime and had the same functionality?
Thanks man!
really freaking cool! modified it to run whenever an in-game explosion happens. Added tonnes of feeling to my project!
Thank you! I put your script in a small Coroutine. Makes my prototypes instantly more fun!
// used fields
private Camera mainCamera;
[SerializeField] private float cameraShakeDuration = 0.5f;
[SerializeField] private float cameraShakeDecreaseFactor = 1f;
[SerializeField] private float cameraShakeAmount = 1f;
// coroutine
IEnumerator ShakeCamera()
{
var originalPos = mainCamera.transform.localPosition;
var duration = cameraShakeDuration;
while(duration > 0)
{
mainCamera.transform.localPosition = originalPos + Random.insideUnitSphere * cameraShakeAmount;
duration -= Time.deltaTime * cameraShakeDecreaseFactor;
yield return null;
}
mainCamera.transform.localPosition = originalPos;
}
Nice script!
For anyone that wants it I made a version that allows you to skip frames to control how jerky the camera shake is, works on any gameObject, and lets you choose whether or not to run it whenever the script is enabled.
public class ObjectShaker : MonoBehaviour {
bool startOnEnable = true;
float shakeDuration = 1f;
float framesToSkip = 0f; //How many frames to we skip in order to slow down the jerkiness of the animation
public float shakeAmount = 1f;
void OnEnable()
{
if(startOnEnable)
{
Shake();
}
}
//Parameters are optional. Defaults to object variables.
public void Shake(float _duration = -1f, float _skip = -1f, float _amount = -1f)
{
if(_duration < 0)
{
_duration = shakeDuration;
}
if(_skip < 0)
{
_skip = framesToSkip;
}
if(_amount < 0)
{
_amount = shakeAmount;
}
StartCoroutine(ShakeCoroutine(_duration, _skip, _amount));
}
IEnumerator ShakeCoroutine(float _duration, float _framesToSkip, float _amount)
{
//For if you need special logic for Main Cameras (e.g. you have a script that follows the player around that you need to disable temporarily)
if(gameObject.tag == "MainCamera")
{
//Disable here
}
Vector3 originalPos = transform.position;
float currFramesSkipped = 0f;
bool locked = true;
while (_duration > 0)
{
if(currFramesSkipped <= _framesToSkip)
{
locked = true;
currFramesSkipped += 1;
} else
{
locked = false;
currFramesSkipped = 0f;
}
if(!locked)
{
transform.position = originalPos + Random.insideUnitSphere * _amount;
}
_duration -= Time.deltaTime;
yield return null;
}
//reenable your camera
if (gameObject.tag == "MainCamera")
{
//reenable camera logic here
}
transform.position = originalPos;
}
}
Thanks :)