//[SerializeField] private Transform rainTransform; //add in later
    //[SerializeField] private ParticleSystem rainSystem; //add in later

    // Use this for initialization
    void Start()
    {
        Destroy(gameObject, destroyDelay); //preprogram to self-destruct


        float x = (transform.localPosition.z);
        float y = (transform.position.y);
        float z = (transform.localPosition.z);

        Vector3 bloopNormalized = new Vector3((x + rainRadius) / (2 * rainRadius), (y - yMin) / (yMax - yMin), (z + rainRadius) / (2 * rainRadius));

        //Debug.Log(bloopNormalized.z);

        if (droplet != null)
        {
            droplet.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Gain, Mathf.Lerp(gainMin, gainMax, bloopNormalized.x));

            if (melodicObj)
            {
                droplet.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Decay, Mathf.Lerp(decayMin, decayMax, bloopNormalized.z));
                droplet.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Oscnote, Mathf.Lerp(oscMin, oscMax, bloopNormalized.y));
                droplet.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Cutoff, Mathf.Lerp(oscMin, oscMax, bloopNormalized.y));
            }
            else
            {
                droplet.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Decay, Mathf.Lerp(decayMin, decayMax, bloopNormalized.y));
                droplet.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Oscnote, Mathf.Lerp(oscMin, oscMax, bloopNormalized.z));
                droplet.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Cutoff, Mathf.Lerp(oscMin, oscMax, bloopNormalized.z));
            }

            droplet.SendEvent(Hv_dropletMax_AudioLib.Event.Bang);
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (positionHistory.Count >= historySize)
        {
            positionAverage = Vector3.LerpUnclamped(positionHistory[0], positionAverage, ((float)positionHistory.Count) / (positionHistory.Count - 1));
            positionHistory.RemoveAt(0);
        }
        if (velocityHistory.Count >= historySize)
        {
            velocityAverage = Mathf.LerpUnclamped(velocityHistory[0], velocityAverage, ((float)velocityHistory.Count) / (velocityHistory.Count - 1));
            velocityHistory.RemoveAt(0);
        }

        /*if (shortVelocityHistory.Count >= shortHistorySize)
         * {
         *  shortVelocityAverage = Mathf.LerpUnclamped(shortVelocityHistory[0], shortVelocityAverage, ((float)shortVelocityHistory.Count) / (shortVelocityHistory.Count - 1));
         *  shortVelocityHistory.RemoveAt(0);
         * }*/

        transform.position = follow.position;

        Vector3 position = transform.position;

        positionHistory.Add(position);

        positionAverage = Vector3.LerpUnclamped(positionHistory[positionHistory.Count - 1], positionAverage, ((float)positionHistory.Count - 1) / positionHistory.Count);

        if (follow.localScale == untrackedJointScale)
        {
            velocityHistory.Add(0);
            velocityAverage = Mathf.LerpUnclamped(velocityHistory[velocityHistory.Count - 1], velocityAverage, ((float)velocityHistory.Count - 1) / velocityHistory.Count);

            //shortVelocityHistory.Add(0);
            //shortVelocityAverage = Mathf.LerpUnclamped(shortVelocityHistory[shortVelocityHistory.Count - 1], shortVelocityAverage, ((float)shortVelocityHistory.Count - 1) / shortVelocityHistory.Count);

            hitToggle = false;
        }
        else
        {
            if (positionHistory.Count > 1)
            {
                float triggerVelocity = (position.y - positionHistory[positionHistory.Count - 2].y) / Time.deltaTime;
                float velocity        = Vector3.Magnitude(position - positionHistory[positionHistory.Count - 2]) / Time.fixedDeltaTime;

                if (velocity < velocityLimit) //check if the velocity is a reasonable value
                {
                    velocityHistory.Add(velocity);
                    velocityAverage = Mathf.LerpUnclamped(velocityHistory[velocityHistory.Count - 1], velocityAverage, ((float)velocityHistory.Count - 1) / velocityHistory.Count);


                    //shortVelocityHistory.Add(velocity);
                    //shortVelocityAverage = Mathf.LerpUnclamped(shortVelocityHistory[shortVelocityHistory.Count - 1], shortVelocityAverage, ((float)shortVelocityHistory.Count - 1) / shortVelocityHistory.Count);


                    if ((triggerVelocity < (thresholdVelocity + thresholdHysterisis)) && !hitToggle)
                    {
                        hitToggle = true;
                    }
                    else if ((triggerVelocity > thresholdVelocity) && hitToggle)
                    {
                        hitToggle = false;

                        synth.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Gain, Mathf.Lerp(gainMin, gainMax, Mathf.Pow((velocityAverage - velocityMin) / (velocityMax - velocityMin), 2)));
                        synth.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Cutoff, Mathf.Lerp(frequencyMin, frequencyMax, (velocityAverage - velocityMin) / (velocityMax - velocityMin)));

                        if (modifyMultiplier)
                        {
                            synth.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Sqrmult, (positionAverage.z - posMin) / (posMax - posMin));
                            synth.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Bandpass, (positionAverage.z - posMin) / (4 * (posMax - posMin)));
                        }
                        if (modifyNote)
                        {
                            synth.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Oscnote, Mathf.Round(Mathf.Lerp(noteMin, noteMax, (positionAverage.x - posMin) / (posMax - posMin))));
                        }

                        synth.SendEvent(Hv_dropletMax_AudioLib.Event.Bang);
                    }
                }
            }
        }
    }
    public override void OnInspectorGUI()
    {
        bool isEnabled = _dsp.IsInstantiated();

        if (!isEnabled)
        {
            EditorGUILayout.LabelField("Press Play!", EditorStyles.centeredGreyMiniLabel);
        }
        // events
        GUI.enabled = isEnabled;
        EditorGUILayout.Space();
        // bang
        if (GUILayout.Button("bang"))
        {
            _dsp.SendEvent(Hv_dropletMax_AudioLib.Event.Bang);
        }

        GUILayout.EndVertical();

        // parameters
        GUI.enabled = true;
        GUILayout.BeginVertical();
        EditorGUILayout.Space();
        EditorGUI.indentLevel++;

        // attack
        GUILayout.BeginHorizontal();
        float attack    = _dsp.GetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Attack);
        float newAttack = EditorGUILayout.Slider("attack", attack, 1.0f, 2000.0f);

        if (attack != newAttack)
        {
            _dsp.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Attack, newAttack);
        }
        GUILayout.EndHorizontal();

        // bandpass
        GUILayout.BeginHorizontal();
        float bandpass    = _dsp.GetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Bandpass);
        float newBandpass = EditorGUILayout.Slider("bandpass", bandpass, 0.0f, 1.0f);

        if (bandpass != newBandpass)
        {
            _dsp.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Bandpass, newBandpass);
        }
        GUILayout.EndHorizontal();

        // cutoff
        GUILayout.BeginHorizontal();
        float cutoff    = _dsp.GetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Cutoff);
        float newCutoff = EditorGUILayout.Slider("cutoff", cutoff, 0.0f, 20000.0f);

        if (cutoff != newCutoff)
        {
            _dsp.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Cutoff, newCutoff);
        }
        GUILayout.EndHorizontal();

        // decay
        GUILayout.BeginHorizontal();
        float decay    = _dsp.GetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Decay);
        float newDecay = EditorGUILayout.Slider("decay", decay, 0.0f, 4000.0f);

        if (decay != newDecay)
        {
            _dsp.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Decay, newDecay);
        }
        GUILayout.EndHorizontal();

        // gain
        GUILayout.BeginHorizontal();
        float gain    = _dsp.GetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Gain);
        float newGain = EditorGUILayout.Slider("gain", gain, 0.0f, 1.0f);

        if (gain != newGain)
        {
            _dsp.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Gain, newGain);
        }
        GUILayout.EndHorizontal();

        // noise
        GUILayout.BeginHorizontal();
        float noise    = _dsp.GetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Noise);
        float newNoise = EditorGUILayout.Slider("noise", noise, 0.0f, 1.0f);

        if (noise != newNoise)
        {
            _dsp.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Noise, newNoise);
        }
        GUILayout.EndHorizontal();

        // oscNote
        GUILayout.BeginHorizontal();
        float oscNote    = _dsp.GetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Oscnote);
        float newOscnote = EditorGUILayout.Slider("oscNote", oscNote, 0.0f, 127.0f);

        if (oscNote != newOscnote)
        {
            _dsp.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Oscnote, newOscnote);
        }
        GUILayout.EndHorizontal();

        // qBase
        GUILayout.BeginHorizontal();
        float qBase    = _dsp.GetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Qbase);
        float newQbase = EditorGUILayout.Slider("qBase", qBase, 0.0f, 20.0f);

        if (qBase != newQbase)
        {
            _dsp.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Qbase, newQbase);
        }
        GUILayout.EndHorizontal();

        // qEnvelope
        GUILayout.BeginHorizontal();
        float qEnvelope    = _dsp.GetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Qenvelope);
        float newQenvelope = EditorGUILayout.Slider("qEnvelope", qEnvelope, 0.0f, 20.0f);

        if (qEnvelope != newQenvelope)
        {
            _dsp.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Qenvelope, newQenvelope);
        }
        GUILayout.EndHorizontal();

        // sawMult
        GUILayout.BeginHorizontal();
        float sawMult    = _dsp.GetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Sawmult);
        float newSawmult = EditorGUILayout.Slider("sawMult", sawMult, 0.0f, 1.0f);

        if (sawMult != newSawmult)
        {
            _dsp.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Sawmult, newSawmult);
        }
        GUILayout.EndHorizontal();

        // sawOffset
        GUILayout.BeginHorizontal();
        float sawOffset    = _dsp.GetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Sawoffset);
        float newSawoffset = EditorGUILayout.Slider("sawOffset", sawOffset, -24.0f, 24.0f);

        if (sawOffset != newSawoffset)
        {
            _dsp.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Sawoffset, newSawoffset);
        }
        GUILayout.EndHorizontal();

        // sinMult
        GUILayout.BeginHorizontal();
        float sinMult    = _dsp.GetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Sinmult);
        float newSinmult = EditorGUILayout.Slider("sinMult", sinMult, 0.0f, 1.0f);

        if (sinMult != newSinmult)
        {
            _dsp.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Sinmult, newSinmult);
        }
        GUILayout.EndHorizontal();

        // sinOffset
        GUILayout.BeginHorizontal();
        float sinOffset    = _dsp.GetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Sinoffset);
        float newSinoffset = EditorGUILayout.Slider("sinOffset", sinOffset, -24.0f, 24.0f);

        if (sinOffset != newSinoffset)
        {
            _dsp.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Sinoffset, newSinoffset);
        }
        GUILayout.EndHorizontal();

        // sqrMult
        GUILayout.BeginHorizontal();
        float sqrMult    = _dsp.GetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Sqrmult);
        float newSqrmult = EditorGUILayout.Slider("sqrMult", sqrMult, 0.0f, 1.0f);

        if (sqrMult != newSqrmult)
        {
            _dsp.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Sqrmult, newSqrmult);
        }
        GUILayout.EndHorizontal();

        // sqrOffset
        GUILayout.BeginHorizontal();
        float sqrOffset    = _dsp.GetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Sqroffset);
        float newSqroffset = EditorGUILayout.Slider("sqrOffset", sqrOffset, -24.0f, 24.0f);

        if (sqrOffset != newSqroffset)
        {
            _dsp.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Sqroffset, newSqroffset);
        }
        GUILayout.EndHorizontal();

        // triMult
        GUILayout.BeginHorizontal();
        float triMult    = _dsp.GetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Trimult);
        float newTrimult = EditorGUILayout.Slider("triMult", triMult, 0.0f, 1.0f);

        if (triMult != newTrimult)
        {
            _dsp.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Trimult, newTrimult);
        }
        GUILayout.EndHorizontal();

        // triOffset
        GUILayout.BeginHorizontal();
        float triOffset    = _dsp.GetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Trioffset);
        float newTrioffset = EditorGUILayout.Slider("triOffset", triOffset, -24.0f, 24.0f);

        if (triOffset != newTrioffset)
        {
            _dsp.SetFloatParameter(Hv_dropletMax_AudioLib.Parameter.Trioffset, newTrioffset);
        }
        GUILayout.EndHorizontal();
        EditorGUI.indentLevel--;
    }