OldBlueWater/BlueWater/Assets/Crest/Crest-Examples/Shared/Scripts/TimedDestroy.cs
2023-08-01 13:03:57 +09:00

62 lines
1.6 KiB
C#

// Crest Ocean System
// Copyright 2020 Wave Harmonic Ltd
using UnityEngine;
namespace Crest.Examples
{
/// <summary>
/// Simple utility script to destroy the gameobject after a set time.
/// </summary>
public class TimedDestroy : MonoBehaviour
{
/// <summary>
/// The version of this asset. Can be used to migrate across versions. This value should
/// only be changed when the editor upgrades the version.
/// </summary>
[SerializeField, HideInInspector]
#pragma warning disable 414
int _version = 0;
#pragma warning restore 414
public float m_lifeTime = 2.0f;
// this seems to make motion stutter?
//public float m_scaleToOneDuration = 0.1f;
public float m_scaleToZeroDuration = 0.0f;
Vector3 m_scale;
float m_birthTime;
void Start()
{
m_birthTime = Time.time;
m_scale = transform.localScale;
}
void Update()
{
float age = Time.time - m_birthTime;
if (age >= m_lifeTime)
{
Helpers.Destroy(gameObject);
}
else if (age > m_lifeTime - m_scaleToZeroDuration)
{
transform.localScale = m_scale * (1.0f - (age - (m_lifeTime - m_scaleToZeroDuration)) / m_scaleToZeroDuration);
}
/*else if ( age < m_scaleToOneDuration && m_scaleToOneDuration > 0.0f )
{
transform.localScale = m_scale * age / m_scaleToOneDuration;
}*/
else
{
transform.localScale = m_scale;
}
}
}
}