using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Fader : MonoBehaviour
{
public CanvasGroup canvasGroup;
public GameObject objectToScale;
public float duration = 1f;
public Vector3 minSize;
public Vector3 maxSize;
public bool scaleUp = false;
public Coroutine scaleCoroutine;
public bool isAutomatic = false;
private bool automatic = true;
private void Start()
{
objectToScale.transform.localScale = minSize;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.G))
{
Fade();
}
if (isAutomatic && automatic)
{
Fade();
automatic = false;
}
}
private IEnumerator ScaleOverTime(GameObject targetObj,
Vector2 lerpto/*Vector3 toScale*/, float duration)
{
float counter = 0;
Vector3 startScaleSize = targetObj.transform.localScale;
while (counter < duration)
{
counter += Time.deltaTime;
//targetObj.transform.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);
canvasGroup.alpha = Mathf.Lerp(lerpto.x, lerpto.y, counter / duration);
yield return null;
}
yield return null;
automatic = true;
}
private void Fade()
{
scaleUp = !scaleUp;
if (scaleCoroutine != null)
{
StopCoroutine(scaleCoroutine);
}
if (scaleUp)
{
scaleCoroutine = StartCoroutine(ScaleOverTime(objectToScale, maxSize, duration));
}
else
{
scaleCoroutine = StartCoroutine(ScaleOverTime(objectToScale, minSize, duration));
}
}
}
it was working fine when i used Vector3.Lerp to scale object up/down :
targetObj.transform.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);
I pressed the G key and while it was scaling up or down i pressed G again in the middle and it was changing the scaling direction from the current point.
but once i changed it to Mathf.Lerp :
canvasGroup.alpha = Mathf.Lerp(lerpto.x, lerpto.y, counter / duration);
to change canvasgroup alpha from 0 to 1 when i press the G key and it’s fading up or down and then pressing G key in the middle it’s starting from the changed direction and not continue to the new changed direction.
how can i make that it will also continue when changing the direction/s using the Mathf.Lerp ?