优化敌人碰到死亡时的表现效果
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace IndianOceanAssets.Engine2_5D
|
||||
{
|
||||
/// <summary>
|
||||
/// 怪物灵灯光照响应 —— 挂到敌人预制体上。
|
||||
/// 当怪物被灵灯碰触时,在敌人位置创建一个独立光源,亮起 3 秒后消失。
|
||||
/// 光源独立于敌人存在,即使敌人立即死亡也能正常显示。
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(HealthSystem))]
|
||||
public class EnemyLanternLight : MonoBehaviour
|
||||
{
|
||||
[Header("光照参数")]
|
||||
[Tooltip("亮起持续时间(秒)")]
|
||||
[SerializeField] private float lightDuration = 3f;
|
||||
|
||||
[Tooltip("亮起时的光照半径")]
|
||||
[SerializeField] private float lightRadius = 3f;
|
||||
|
||||
[Tooltip("亮起时的光照强度")]
|
||||
[SerializeField] private float lightIntensity = 0.8f;
|
||||
|
||||
/// <summary>
|
||||
/// 由 SpiritLantern 在碰触时调用。
|
||||
/// 在敌人当前位置创建一个独立光源,3秒后自动销毁。
|
||||
/// </summary>
|
||||
public void ActivateLight()
|
||||
{
|
||||
// 在敌人位置创建独立光源物体
|
||||
GameObject lightObj = new GameObject("EnemyLanternGlow");
|
||||
lightObj.transform.position = transform.position;
|
||||
|
||||
// 添加 LightSource 组件
|
||||
LightSource ls = lightObj.AddComponent<LightSource>();
|
||||
ls.SetRadius(lightRadius);
|
||||
ls.SetIntensity(lightIntensity);
|
||||
|
||||
// 启动自动销毁协程(挂在独立物体上,不受敌人死亡影响)
|
||||
lightObj.AddComponent<AutoDestroyLight>().Initialize(ls, lightDuration);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 内部辅助组件:光源亮起后渐隐并自动销毁。
|
||||
/// </summary>
|
||||
internal class AutoDestroyLight : MonoBehaviour
|
||||
{
|
||||
private LightSource _lightSource;
|
||||
private float _duration;
|
||||
private float _elapsed;
|
||||
|
||||
public void Initialize(LightSource ls, float duration)
|
||||
{
|
||||
_lightSource = ls;
|
||||
_duration = duration;
|
||||
_elapsed = 0f;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
_elapsed += Time.deltaTime;
|
||||
float t = Mathf.Clamp01(_elapsed / _duration);
|
||||
|
||||
if (_lightSource != null)
|
||||
{
|
||||
// 前 70% 保持全亮,后 30% 渐隐
|
||||
float fadeT = Mathf.Clamp01((t - 0.7f) / 0.3f);
|
||||
_lightSource.SetIntensity(Mathf.Lerp(0.8f, 0f, fadeT));
|
||||
}
|
||||
|
||||
if (t >= 1f)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user