Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FogRemover : MonoBehaviour
{
public bool canUseLantern;
private bool hasLantern;
public GameObject Lantern;
[SerializeField] private int particleAmount;
private ParticleSystem smoke;
public float particleAmountMax = 1000;
private Coroutine regen;
private WaitForSeconds regenTick = new WaitForSeconds(0.001f);
private float defaultLight;
private GameObject otherObject;
public Animator anim;
private void Start()
{
anim = GetComponent<Animator>();
defaultLight = Lantern.GetComponentInChildren<Light>().intensity;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Fog" && this.gameObject.tag == "Player")
{
otherObject = other.gameObject;
smoke = other.transform.GetChild(0).GetComponent<ParticleSystem>();
if ( particleAmount == 0)
{
var main = smoke.main;
particleAmount = main.maxParticles;
}
Transform[] children = transform.GetComponentsInChildren<Transform>();
foreach (var child in children)
{
if (child.name == "rig1:l_hand") // Find node that lantern is attached to
{
GameObject TheLantern = child.transform.Find("SK_Lantern").gameObject;
Light lit = transform.GetComponentInChildren<Light>();
if (TheLantern.activeInHierarchy)
{
if (lit.enabled && TheLantern.GetComponentInChildren<Light>().enabled == true)
{
canUseLantern = true;
}
}
}
}
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Fog" && this.gameObject.tag == "Player")
{
canUseLantern = false;
}
}
private void FixedUpdate()
{
if (Lantern.activeInHierarchy)
{
Light lite = Lantern.transform.GetComponentInChildren<Light>();
if (Lantern.GetComponentInChildren<Light>().enabled == true)
{
hasLantern = true;
}
}
if (Input.GetKey(KeyCode.F))
{
anim.SetBool("FogRemove", true);
if (canUseLantern && hasLantern)
{
Lantern.GetComponentInChildren<Light>().intensity = 40000f;
//Lantern.GetComponent<ParticleSystem>().Play();
if (particleAmount > 0)
{
RemoveFog();
}
}
if (hasLantern && !canUseLantern)
{
Lantern.GetComponentInChildren<Light>().intensity = 40000f;
Lantern.GetComponentInChildren<Light>().GetComponent<LightFLicker>().enabled = true;
}
}
else
{
anim.SetBool("FogRemove", false);
Lantern.GetComponentInChildren<Light>().intensity = defaultLight;
Lantern.GetComponentInChildren<Light>().GetComponent<LightFLicker>().enabled = false;
//Lantern.GetComponent<ParticleSystem>().Stop();
}
if (Input.GetKeyDown(KeyCode.F) && canUseLantern)
{
gameObject.GetComponent<AudioSource>().Play();
}
if (Input.GetKeyUp(KeyCode.F))
{
gameObject.GetComponent<AudioSource>().Stop();
}
if ( particleAmount <= 0)
{
if (otherObject != null) {
otherObject.gameObject.SetActive(false);
canUseLantern = false;
//Lantern.GetComponentInChildren<Light>().enabled = false;
anim.SetBool("FogRemove", false);
otherObject = null;
gameObject.GetComponent<AudioSource>().Stop();
Lantern.GetComponent<LanternController>().ExtinguishLantern();
StopCoroutine(regen);
}
}
}
void RemoveFog()
{
particleAmount -= 4;
var main = smoke.main;
main.maxParticles = particleAmount;
if (regen != null)
{
StopCoroutine(regen);
}
regen = StartCoroutine(RegenStamina());
}
private IEnumerator RegenStamina()
{
yield return new WaitForSeconds(1);
while (particleAmount < particleAmountMax)
{
particleAmount += 10;
var main = smoke.main;
main.maxParticles = particleAmount;
yield return regenTick;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment