Add CameraShake component and integrate with Health system; implement shake effect on damage taken

This commit is contained in:
2025-07-12 16:58:42 +02:00
parent 56592fa7ad
commit 4b68446ea9
6 changed files with 965 additions and 2 deletions

View 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;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 41eb673f574247489f63e14a61a3e004
timeCreated: 1752331487

View File

@@ -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();
}
}
}