Files
blockout/BlockOutReplica/Assets/Scripts/Presentation/BlockFxParticle.cs
T
2026-10-20 13:21:04 +08:00

36 lines
1.2 KiB
C#

using UnityEngine;
namespace BlockOutReplica.Presentation
{
public sealed class BlockFxParticle : MonoBehaviour
{
private Vector3 velocity;
private Vector3 angularVelocity;
private float lifetime;
private float age;
private float gravity;
private Vector3 startScale;
public void Initialize(Vector3 initialVelocity, float duration, float gravityAmount)
{
velocity = initialVelocity;
lifetime = Mathf.Max(.05f, duration);
gravity = gravityAmount;
angularVelocity = Random.insideUnitSphere * 320f;
startScale = transform.localScale;
}
private void Update()
{
age += Time.deltaTime;
float t = Mathf.Clamp01(age / lifetime);
velocity += Vector3.down * gravity * Time.deltaTime;
transform.position += velocity * Time.deltaTime;
transform.Rotate(angularVelocity * Time.deltaTime, Space.Self);
float scale = Mathf.Sin(Mathf.Clamp01(t * 1.35f) * Mathf.PI) * (1f - t * .35f);
transform.localScale = startScale * Mathf.Max(0f, scale);
if (age >= lifetime) Destroy(gameObject);
}
}
}