0
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Outline : MonoBehaviour
{
    public float width;

    private Renderer rend;
    private float currentWidth;

    private void Start()
    {
        rend = GetComponent<Renderer>();
        currentWidth = rend.material.GetFloat("_Outline");

        StartCoroutine(ChangeOutline());
    }

    private void Update()
    {
        //SetOutLineWidth(width);
    }

    public void SetOutLineWidth(float width)
    {
        rend.material.SetFloat("_Outline", width);
    }

    IEnumerator ChangeOutline()
    {
        float timeToWait = 3f;

        while (currentWidth < width)
        {
            width += 0.01f;
            SetOutLineWidth(width);
            yield return new WaitForSeconds(timeToWait);
        }
    }
}

What I want to do is for example if I will set width variable value to 5 then change the value until the currentWidth is 5:

SetOutLineWidth(width);

If the currentWidth is 7 and I set width to 5 change the value from 7 to 5 slowly. If currentWidth is 7 and I set width to 10 again change it slowly to 10. What I did now is making the width value keep raising and setting the Outline non stop.

What I want to do in general is to make two modes:

  1. Change the width value and set the Outline slowly according to the time once on one direction.

  2. Like 1 but make it ping pong mode once the outline value reached to width change it back slowly to the first value outline was and then back to width nonstop.

\$\endgroup\$
0

2 Answers 2

2
\$\begingroup\$

I guess what you are trying to do (based, on your description) is to lerp between 2 numbers.

No need for timer, just do this:

public float duration = 3f;
float lerp = 0;

public float currentWidth = 4;
public float finalWidth = 10;


private void Update()
{
    lerp += Time.deltaTime / duration;
    currentWidth = Mathf.Lerp(currentWidth, finalWidth, lerp);

    SetOutLineWidth(currentWidth);
}

public void SetOutLineWidth(float w)
{
    rend.material.SetFloat("_Outline", w);
}

Unity Lerp documentation: https://docs.unity3d.com/ScriptReference/Mathf.Lerp.html

\$\endgroup\$
1
\$\begingroup\$

I don't know what material you are using, so I just changed the script to use the scale of a plane object, but you can attach any shape to it. You should be able to modify the script to make it work with the material property.

using UnityEngine;

public enum Mode
{
    Grow,
    Shrink,
    PingPong
}

public class Outline : MonoBehaviour {

public float maxWidth;
public float minWidth;
public float growthRate;
public float timeToWait = 1f;
public Mode Mode;

private float currentWidth;
private bool grow = false;
private float timeToUpdate;

private void Update()
{
    currentWidth = transform.localScale.x;
    if (Time.time + timeToWait > timeToUpdate)
    {
        switch (Mode)
        {
            case Mode.Grow:
                if (currentWidth < maxWidth)
                {
                    currentWidth += growthRate;
                    SetOutLineWidth(currentWidth);
                }

                break;
            case Mode.Shrink:
                if (currentWidth > minWidth)
                {
                    currentWidth -= growthRate;
                    SetOutLineWidth(currentWidth);

                }

                break;
            case Mode.PingPong:

                if (grow)
                {
                    currentWidth += growthRate;
                    SetOutLineWidth(currentWidth);
                   if (currentWidth >= maxWidth)
                       grow = false;
                }
                else
                {
                    currentWidth -= growthRate;
                    SetOutLineWidth(currentWidth);
                   if (currentWidth <= minWidth)
                        grow = true;

                }

                break;
            default:
                break;
        }

        timeToUpdate = Time.time + timeToWait;
    }
}

public void SetOutLineWidth(float width)
{
    transform.localScale = new Vector3(width, 1, 1);
}
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .