Files
VRProject/Assets/_Scripts/EnemyHealth.cs
oogushiyuuga 4d3fee6ca5 いろいろやってみた
Playerが蜘蛛にめり込まないようにしたい
2026-07-01 16:23:04 +09:00

69 lines
1.4 KiB
C#

using System;
using UnityEngine;
public class EnemyHealth : MonoBehaviour, IDamageble
{
[SerializeField] private float maxHealth = 100f;
private float currentHealth;
[SerializeField] private float knockbackDuration = 0.5f;
private EnemyAI enemyAI;
private bool isDead = false;
void Start()
{
currentHealth = maxHealth; //HPを代入
enemyAI = GetComponent<EnemyAI>();
}
public void TakeDamage(DamageInfo damageInfo)
{
if(currentHealth <= 0) return;
currentHealth -= damageInfo.amount; //ダメージ計算
if(damageInfo.isPoseBonus)
{
Debug.Log("構えボーナス");
}
if(currentHealth <= 0)
{
Die();
}
else
{
if(enemyAI != null)
{
enemyAI.TriggerKnockback(knockbackDuration);
}
}
}
private void Die()
{
if(isDead) return;
isDead = true;
Animator anim = GetComponent<Animator>();
if(anim != null)
{
anim.SetTrigger("Dead");
}
Collider col = GetComponent<Collider>();
if(col != null)
{
col.enabled = false;
}
if(enemyAI != null)
{
enemyAI.DisableAIOnDeath();
}
Destroy(gameObject, 3.0f);
}
}