CameraShake

Camera shake is working now
This commit is contained in:
Noah4ever
2021-12-13 16:02:11 +01:00
parent a2ac91bf6e
commit b2b997307b
7 changed files with 174 additions and 49 deletions

36
Assets/CameraShake.cs Normal file
View File

@@ -0,0 +1,36 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraShake : MonoBehaviour {
[Header("Camera Shake Info")]
[SerializeField] bool cameraShakeActive = true;
[SerializeField] Camera objectToMove;
[SerializeField] float cameraShakeRadius = 6f;
[SerializeField] float cameraShakeDuration = 1f;
[SerializeField] AnimationCurve cameraShakeCurve;
void cameraShake() {
Collider[] colliders = Physics.OverlapSphere(transform.position, cameraShakeRadius);
foreach (Collider nearbyObject in colliders) {
if (nearbyObject.GetComponent<Player>() && nearbyObject.GetType() != typeof(UnityEngine.CharacterController)) {
// Start coroutine that shakes the camera
StartCoroutine(shaking(nearbyObject));
}
}
}
IEnumerator shaking(Collider obj) {
float elapsedTime = 0f;
while (elapsedTime < cameraShakeDuration) {
elapsedTime += Time.deltaTime;
float strength = cameraShakeCurve.Evaluate(elapsedTime / cameraShakeDuration);
objectToMove.transform.localPosition = objectToMove.transform.localPosition + Random.insideUnitSphere * strength;
yield return new WaitForSeconds(Time.deltaTime);
}
yield return null;
}
}