- Home /
Increment a variable by using an AnimationCurve graph
Hi.
I need some sort of a formula to increase an integer/float to 100 within a specified time, let's say 60 seconds.
I don't want it to increase linearly (add 10 every 6 seconds), but I want it to increase based on an AnimationCurve graph. This is very important.
This is my actual AnimationCurve graph
So in this graph, the x represents the time and f(x) a value. For instance, the variable would increase a lot from 0.3 to 0.85, and only a little from 0 to 0.3 and from 0.85 to 1.
I've tried a lot of random formulas but it didn't go well.
Does anyone have an idea on how to do this ?
Edit 1 : I've managed to do something : what I've done is that I have a coroutine and its code is executed once every second. Then it calculates the percent of chance the value will be incremented (based on the curve so if the time is 0.3 it will have near 50% chances to be incremented). If not, add the value in a variable and retry the next second. If the next second the condition is true, it gets incremented by the sum.
The problem is that at the end the curve is going down, so the probability too. So the sum may never be added. So I've added to the code that if time > 1 then add the sum. It's still not what I want but at least it is something.
using UnityEngine;
using System.Collections;
using System;
public class LikesManager : MonoBehaviour
{
public AnimationCurve curve;
IEnumerator Start()
{
System.Random rnd = new System.Random();
DateTime myDate = DateTime.Now;
float totalTime = 60;
int total = 100;
float incrementingByThisSecond = total / totalTime;
float varToBeIncremented = 0;
while (true)
{
yield return new WaitForSeconds(1f);
TimeSpan diff = DateTime.Now.Subtract(myDate);
if (diff.TotalSeconds <= totalTime + 1)
{
float secondsToGraph = (float)diff.TotalSeconds / totalTime;
if (rnd.Next(0, 100) < Mathf.RoundToInt(curve.Evaluate(secondsToGraph) * 100))
{
varToBeIncremented += incrementingByThisSecond;
incrementingByThisSecond = total / totalTime;
Debug.Log("Ok, new value = " + incrementingByThisSecond);
}
else
{
incrementingByThisSecond += total / totalTime;
Debug.Log("Not ok, new value = " + incrementingByThisSecond);
}
Debug.Log("Var = " + varToBeIncremented);
}
else
{
varToBeIncremented += incrementingByThisSecond;
incrementingByThisSecond = 0;
Debug.Log("The end, var = " + varToBeIncremented);
}
}
}
}
You can use AnimationCurve.Evaluate with Time.time
as parameter. Not sure if this is what you are looking for.
Answer by Bieere · Dec 11, 2016 at 10:25 PM
When using Animationcurve.Evaluate() the value you evaluate is the position along the curve, relative to the x axis, which then returns the relative y value.
For example:
Animationcurve.Evaluate(0.5f) on your graph would return ~0.95f
Your answer
Follow this Question
Related Questions
3D Grapher - Function input problem 1 Answer
Find angle between two gameobjects? 1 Answer
How I can increase num 1 to num 2 in x time? 1 Answer
Distribute terrain in zones 3 Answers
Calculate surface under a curve from an animationcurve 3 Answers