Add CameraShake component and integrate with Health system; implement shake effect on damage taken
This commit is contained in:
48
Assets/Scripts/Systems/CameraShake.cs
Normal file
48
Assets/Scripts/Systems/CameraShake.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using KBCore.Refs;
|
||||
using Unity.Cinemachine;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Systems
|
||||
{
|
||||
public class CameraShake : MonoBehaviour
|
||||
{
|
||||
private float timer;
|
||||
private bool isShaking;
|
||||
|
||||
[SerializeField] private float duration;
|
||||
[SerializeField, Self] private CinemachineBasicMultiChannelPerlin noise;
|
||||
[SerializeField] private Health health;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
health.OnTakeDamage += Shake;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
health.OnTakeDamage -= Shake;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!isShaking || !noise) return;
|
||||
if (timer <= 0f)
|
||||
{
|
||||
isShaking = false;
|
||||
noise.AmplitudeGain = 0f;
|
||||
} else
|
||||
{
|
||||
timer -= Time.deltaTime;
|
||||
}
|
||||
}
|
||||
|
||||
private void Shake()
|
||||
{
|
||||
if (!noise) return;
|
||||
isShaking = true;
|
||||
timer = duration;
|
||||
noise.AmplitudeGain = 1f;
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Scripts/Systems/CameraShake.cs.meta
Normal file
3
Assets/Scripts/Systems/CameraShake.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 41eb673f574247489f63e14a61a3e004
|
||||
timeCreated: 1752331487
|
@@ -14,6 +14,8 @@ namespace Systems
|
||||
|
||||
public GameObject LastAttacker => lastAttacker;
|
||||
|
||||
public event Action OnTakeDamage;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
character.attributes.SetHealth(initialHealth);
|
||||
@@ -24,6 +26,7 @@ namespace Systems
|
||||
lastAttacker = attacker;
|
||||
var effectiveDamage = Math.Max(damage - character.attributes.Armor, 1);
|
||||
character.attributes.ModifyHealth(-effectiveDamage);
|
||||
OnTakeDamage?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user