I’m making a side scrolling game but i’ve set up the camera using animation to give it a more cinematic feel, the problem is I had a script setup to shake the screen if the player took damage and now im using the animator the screen shake doesn’t work. Is there any way to implement the shake without temporarily disabling the animator component? My shake script:
using UnityEngine;
using System.Collections;
public class CameraShake : MonoBehaviour
{
public static CameraShake instance;
private Vector3 _originalPos;
private float _timeAtCurrentFrame;
private float _timeAtLastFrame;
private float _fakeDelta;
void Awake()
{
instance = this;
}
void Update()
{
// Calculate a fake delta time, so we can Shake while game is paused.
_timeAtCurrentFrame = Time.realtimeSinceStartup;
_fakeDelta = _timeAtCurrentFrame - _timeAtLastFrame;
_timeAtLastFrame = _timeAtCurrentFrame;
}
public static void Shake(float duration, float amount)
{
instance._originalPos = instance.gameObject.transform.localPosition;
instance.StopAllCoroutines();
instance.StartCoroutine(instance.cShake(duration, amount));
}
public IEnumerator cShake(float duration, float amount)
{
float endTime = Time.time + duration;
while (duration > 0)
{
transform.localPosition = _originalPos + Random.insideUnitSphere * amount;
duration -= _fakeDelta;
yield return null;
}
transform.localPosition = _originalPos;
}
}