Lerp takes a starting value, and ending value, and a clamped interpolation (0…1) between the two.
In a coroutine…
IEnumerator SimpleLerp(){
int a = 0; // start
int b = 1; // end
int x = 5; // time frame
float n = 0; // lerped value
for(float f = 0; f <= x; f += Time.deltaTime) {
n = Mathf.Lerp(a, b, f / x); // passing in the start + end values, and using our elapsed time 'f' as a portion of the total time 'x'
// use 'n' .. ?
yield return null;
}
}
public Color TurnObjectsVis()
{
for (int i = 0; i < GraphicsObject.Count; i++)
{
GraphicsObject[i].SetActive(true);
if (GraphicsObject[i].GetComponent<SpriteRenderer>() != null)
{
sr = GraphicsObject[i].GetComponent<SpriteRenderer>();
sr.color = Color.Lerp(new Color(sr.color.r, sr.color.g, sr.color.b, sr.color.a), new Color(sr.color.r, sr.color.g, sr.color.b, 255), 0.0000100f);
if (GraphicsObject[i].gameObject.GetComponent<ParticleSystemRenderer>() != null)
{
ParticleSystemRenderer psr = GraphicsObject[i].GetComponent<ParticleSystemRenderer>();
Material mr = psr.material;
mr.color = Color.Lerp(new Color(mr.color.r, mr.color.g, mr.color.b, mr.color.a), new Color(mr.color.r, mr.color.g, mr.color.b, 255), 0.0000100f); // i want to replace that horrible 0.0000100f with the value
}
if (sr.color.a >= 1f)
{
this.gameObject.GetComponent<Collider>().enabled = true;
}
}
}
return sr.color;
}
mmh i decided to move alot of stuff to Corutines as they actually do less checking and less work then update. i am prob going to need like 10 corutines but that shouldn’t be bad?
float lerpingValue;
float A, B;
float time, duration;
bool isLerping;
public void MyLerp(float A, float B, float X)
{
this.A = A;
this.B = B;
this.duration = X;
this.time = 0;
this.isLerping = true;
// 'move per second' value can be calculated like this
float movePerSecond = (B - A) / X;
}
void Update()
{
if (isLerping)
{
// Accumulating time
time += Time.deltaTime;
// determining where are we on the time line
// flow is our time line increasing from 0 to 1
float flow = time / duration;
if (flow < 1)
{
// lerping formula
lerpingValue = A + (B - A) * flow;
}
else
{
// operation is done
lerpingValue = B;
isLerping = false;
}
}
}
You can call it like this:
MyLerp(0, 1, 5);
Tell me if I understood something wrong.
Hope it helps
its somewhat what i tried to do. and i ended up trying to use corutines but i quickly discovered with my system i build atm i can’t really do that without doing to much extra code.
i would diff like to move my code out of a constant update loop. so i can get more control over the code.
However you write your code, it is gonna run in something like an update loop. Either coroutines or actual update methods run every frame. So there is not much of a choice here.
But about having control over your code, what do you want to do exactly? We can modify this code to fulfill your needs.