0

I have a script that instantiates a prefab when a projectile first enters a collider, and then increases the scale of that prefab for each of the next four projectiles that enter the collider using Lerps.

The interpolation is fine the first time this code runs. But each time after that there is a delay in the first Lerp function. How do I fix this awkward pause?

An example gif of the problem

private void OnTriggerEnter(Collider other)
{
    if(other.CompareTag("Projectile"))
    {
        catchCount++;
    }
}

void CatchAndRelease()
{
    proj = GameObject.Find("ReleaseObj(Clone)");

    switch (catchCount)
    {
        case 0:
            if(isInstantiated == true)
            {
                catchCount = 0;
            }
            break;
        case 1:
            if(isInstantiated == false)
            {
                Instantiate(releaseObj, transform.position, Quaternion.identity, gameObject.transform.parent);
                isInstantiated = true;
            }
            break;
        case 2:
            Vector3 scaleUp = new Vector3(1.5f, 1.5f, 1.5f);
            proj.transform.localScale = Vector3.Lerp(proj.transform.localScale, scaleUp, Time.deltaTime);
            break;
        case 3:
            Vector3 scaleUp2 = new Vector3(2f, 2f, 2f);
            proj.transform.localScale = Vector3.Lerp(proj.transform.localScale, scaleUp2, Time.deltaTime);
            break;
        case 4:
            Vector3 scaleUp3 = new Vector3(2.5f, 2.5f, 2.5f);
            proj.transform.localScale = Vector3.Lerp(proj.transform.localScale, scaleUp3, Time.deltaTime);
            break;
        case 5:
            Vector3 scaleUp4 = new Vector3(3f, 3f, 3f);
            proj.transform.localScale = Vector3.Lerp(proj.transform.localScale, scaleUp4, Time.deltaTime);

            break;
        case 6:
            if(isInstantiated == false)
            {
                catchCount = 0;

            }
            else
            {
                catchCount = 6;
            }
            break;

}

Still quite new to C# and any help or advice is greatly appreciated!

Community
  • 1
  • 1

1 Answers1

1

Lerp is meant to be linear, and in fact the L stands for linear. The issue with the above code is that the first parameter, transform.localScale, changes every frame, so the time it takes to go from the current transform.localScale and the target scale would decrease over time. Here are some links on better ways of using Lerp:

http://www.blueraja.com/blog/404/how-to-use-unity-3ds-linear-interpolation-vector3-lerp-correctly

http://theantranch.com/blog/using-lerp-properly/

You essentially need to declare a variable containing the localScale before you use Lerp, and use that variable as the first parameter.

Hope this helps!

Antoine
  • 141
  • 1
  • 12