LERP not working with WindZone

Trying to create a smooth burst of wind when an explosion happens, but my LERP is not working. Instead of smoothly transitioning it just instantly jumps from 0 to 1 in a single frame. I know the values are getting set properly, as I can visually see it in the editor. It just isn’t doing the LERP. (Using 2019.3b5)
Here is my code. Any idea what I’m doing wrong here?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using CoverShooter;

public class ExplosionWind : MonoBehaviour
{
    public float MinStart = 0f;
    public float MaxVal = 1f;
    public WindZone AttachedWindZone;
    public DelayedDestroy AttachedDelayDestroy;
    public float DestroyTime = 4f;

    // Start is called before the first frame update
    void Awake()
    {
        getWindObject();
        SetStartVal();
        getDelayedDestroy();
        SetDestroyTime();
    }

    // Update is called once per frame
    void Update()
    {
        var timer = Time.deltaTime;
        if (timer <= DestroyTime) { TimerUnderHalf(); }
        if (timer >= DestroyTime) { TimerOverHalf();  }
    }

    void getWindObject()
    {
        if (AttachedWindZone == null)
        {
            AttachedWindZone = GetComponent<WindZone>();
        }
        else return;

    }

    void getDelayedDestroy()
    {
        if (AttachedDelayDestroy == null)
        {
            AttachedDelayDestroy = GetComponent<DelayedDestroy>();
        }
        else return;
    }

    void SetStartVal()
    {
        AttachedWindZone.windTurbulence = 0f;
        AttachedWindZone.windMain = 0f;
        AttachedWindZone.windPulseMagnitude = 0f;
        AttachedWindZone.windPulseFrequency = 0f;
    }

    void SetDestroyTime()
    {
        DestroyTime = AttachedDelayDestroy.Delay/2;
    }

    void TimerUnderHalf()
    {
        AttachedWindZone.windTurbulence = Mathf.Lerp(MinStart, MaxVal, DestroyTime);
        AttachedWindZone.windMain = Mathf.Lerp(MinStart, MaxVal, DestroyTime);
        AttachedWindZone.windPulseMagnitude = Mathf.Lerp(MinStart, MaxVal, DestroyTime);
        AttachedWindZone.windPulseFrequency = Mathf.Lerp(MinStart, MaxVal, DestroyTime);
    }

    void TimerOverHalf()
    {
        AttachedWindZone.windTurbulence = Mathf.Lerp(MaxVal, MinStart, DestroyTime);
        AttachedWindZone.windMain = Mathf.Lerp(MaxVal, MinStart, DestroyTime);
        AttachedWindZone.windPulseMagnitude = Mathf.Lerp(MaxVal, MinStart, DestroyTime);
        AttachedWindZone.windPulseFrequency = Mathf.Lerp(MaxVal, MinStart, DestroyTime);
    }

}

The Lerp is doing what you are telling it to. Lerp linearly interpolates between two values by the t parameter which is the 3rd value you pass. If you want it to be smooth, then you would need to pass a value which increases from 0 to 1 each frame until it is complete.

1 Like

Take a look at this code. It’s typically how it’s done. A lot of people like to put lerps in coroutines, because they just fit well conceptually together. You’re doing a little bit of something every frame until you reach your goal. That’s what a coroutine is for.

IEnumerator AnimateMove(Vector3 origin, Vector3 target, float duration)
{
  float journey = 0f;
  while (journey <= duration)
  {
    journey = journey + Time.deltaTime;
    float percent = Mathf.Clamp01(journey / duration);
   
    transform.position = Vector3.Lerp(origin, target, percent);
   
    yield return null;
  }
}
void MoveTo(Vector3 target, float duration)
{
  StartCoroutine(AnimateMove(transform.position, target, duration));
}

Thx folks,
This is the solution I ended up going with…or at least one that technically worked…except it doesnt work with ATG. :frowning: Oh well. Pasting it here anyway in case someone else wants it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using CoverShooter;

public class ExplosionWind : MonoBehaviour
{
    public float MinStart = 0f;
    public float MaxVal = 1f;
    public WindZone AttachedWindZone;
    public DelayedDestroy AttachedDelayDestroy;
    public float DestroyTime = 4f;
    public float LerpSpeed;
    public float timer;

    // Start is called before the first frame update
    void Awake()
    {
        getWindObject();
        SetStartVal();
        getDelayedDestroy();
        SetDestroyTime();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        timer += Time.deltaTime;
        if (timer < DestroyTime/2) { TimerUnderHalf(); }
        else if (timer >= DestroyTime/2) { TimerOverHalf();  }
       
    }

    void getWindObject()
    {
        if (AttachedWindZone == null)
        {
            AttachedWindZone = GetComponent<WindZone>();
        }
        else return;

    }

    void getDelayedDestroy()
    {
        if (AttachedDelayDestroy == null)
        {
            AttachedDelayDestroy = GetComponent<DelayedDestroy>();
        }
        else return;
    }

    void SetStartVal()
    {
        AttachedWindZone.windTurbulence = 0f;
        AttachedWindZone.windMain = 0f;
    }

    void SetDestroyTime()
    {
        DestroyTime = (float)AttachedDelayDestroy.Delay;
        LerpSpeed = ((DestroyTime / 2f)*.01f);
    }

    void TimerUnderHalf()
    {
        AttachedWindZone.windTurbulence =       Mathf.Lerp(AttachedWindZone.windTurbulence, MaxVal, LerpSpeed);
        AttachedWindZone.windMain =             Mathf.Lerp(AttachedWindZone.windMain, MaxVal, LerpSpeed);
    }

    void TimerOverHalf()
    {
        AttachedWindZone.windTurbulence =       Mathf.Lerp(AttachedWindZone.windTurbulence, MinStart, LerpSpeed);
        AttachedWindZone.windMain =             Mathf.Lerp(AttachedWindZone.windMain, MinStart, LerpSpeed);
    }

}