Files
Zombie/Assets/_Zombie/Scripts/Explosions/Explosion.cs

58 lines
1.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Collections;
using System.Collections.Generic;
using DarkTonic.MasterAudio;
using DragonLi.Core;
using Mirror;
using UnityEngine;
public class Explosion : NetworkBehaviour
{
public GameObject[] explosions;
public float radius = 3f; // 检测半径
public LayerMask playerLayer; // 玩家所在的Layer避免检测其他物体
public int damage = 10;
public void Init(int id)
{
foreach (var obj in explosions)
{
obj.SetActive(false);
}
explosions[id].SetActive(true);
switch (id)
{
case 0:
DamagePlayersInRange();
break;
}
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
{
NetworkServer.Destroy(gameObject);
}, 2f, this);
}
/// <summary>
/// 检测范围内的玩家并执行受伤逻辑
/// </summary>
public void DamagePlayersInRange()
{
Collider[] hits = Physics.OverlapSphere(transform.position, radius, playerLayer);
foreach (Collider hit in hits)
{
Player player = hit.GetComponent<Player>(); // 假设玩家脚本叫 Player
if (player != null)
{
player.ApplyDamage(damage,null,transform); // 调用受伤方法
}
}
}
// 绘制检测范围方便调试
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, radius);
}
}