Files
ihob/Assets/PegLegIo/scripts/DyingPlayerController.cs

54 lines
1.5 KiB
C#
Raw Permalink Normal View History

2026-02-21 17:04:05 -08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DyingPlayerController : MonoBehaviour {
[SerializeField] AudioClip die;
[SerializeField] AudioClip boom;
[SerializeField] float deathTime = 1.5f; // seconds
[SerializeField] Color glowColor = Color.red; // seconds
[SerializeField] float glowFrequency = 10f; // per second
[SerializeField] bool glowing = true;
[SerializeField] GameObject explosion;
AudioSource audioSource;
SpriteRenderer sprite;
// Start is called before the first frame update
void Start() {
}
private void Awake() {
audioSource = GameObject.FindGameObjectWithTag("PegLegIoGame").GetComponent<AudioSource>();
sprite = GetComponent<SpriteRenderer>();
audioSource.PlayOneShot(die);
if (glowing) {
StartCoroutine(Glow());
}
StartCoroutine(Die());
}
private IEnumerator Glow() {
while (true) {
sprite.color = glowColor;
yield return new WaitForSeconds(1.0f / glowFrequency);
sprite.color = Color.white;
yield return new WaitForSeconds(1.0f / glowFrequency);
}
}
private IEnumerator Die() {
yield return new WaitForSeconds(deathTime);
audioSource.PlayOneShot(boom);
GameObject e = Instantiate(explosion);
e.transform.position = transform.position;
Destroy(gameObject);
}
// Update is called once per frame
void Update()
{
}
}