UnityGameProjectsCode / InTheDarkGame / Level / WaterSoundTrigger.cs
WaterSoundTrigger.cs
Raw
using UnityEngine;

public class WaterSoundTrigger : MonoBehaviour
{
    /* When the player steps within the trigger for the water
     * they will create sound which will also leave an empty
     * gameobject marker which can be detected by sound bots.
     * The marker will stay active for some time to allow bots
     * to get near to the origin of the sound. After a while,
     * the marker will remove itself. Each puddle can only have
     * a single marker active at any time.
     */


    public GameObject soundMarker;
    public float timeBeforeMarkerRemove;
    private float timeSincePlayerLeft;
    private bool beginFade;

    private void Awake()
    {
        soundMarker.SetActive(false);
    }

    private void Update()
    {
        if (beginFade)
        {
            timeSincePlayerLeft += Time.deltaTime;

            if (timeSincePlayerLeft >= timeBeforeMarkerRemove)
            {
                soundMarker.SetActive(false);
                beginFade = false;
                timeSincePlayerLeft = 0.0f;
            }
        }
    }

    private void OnTriggerStay2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Player"))
        {
            collision.gameObject.GetComponent<PlayerMove>().SetInWater(true);
            soundMarker.SetActive(true);
            soundMarker.transform.position = collision.gameObject.transform.position;
            beginFade = false;
            timeSincePlayerLeft = 0.0f;
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Player"))
        {
            collision.gameObject.GetComponent<PlayerMove>().SetInWater(false);
            beginFade = true;
        }
    }
}