- Home /
Scripting Curves, How To Maintain The Shape ?
Hello guys, I have a little problem. I am trying to learn how to script AnimationCurves, in order to change my Color Correction Curves Image Effect in runtime. I've figured out the most of it, but still I have a minor glitch to solve. First, here is the default value and the desired value of my red channel :
So I wrote this script:
using UnityEngine;
using System.Collections;
using UnityStandardAssets.ImageEffects;
public class IS_ActionImageEffects : MonoBehaviour {
public Transform t_EffectsHolder;
public float f_LerpSpeedRed;
public float f_LerpSpeedRedBack;
public bool b_WaitFirst;
public float f_WaitFirst;
public float f_WaitToLerpBack;
private ColorCorrectionCurves _CorrectionCurves;
private Vector2 v2_OriginalRedKey;
void Awake()
{
_CorrectionCurves = t_EffectsHolder.GetComponent<ColorCorrectionCurves>();
}
void OnEnable()
{
StartCoroutine(LerpRedChannel());
}
IEnumerator LerpRedChannel()
{
if (b_WaitFirst)
yield return new WaitForSeconds(f_WaitFirst);
float i = 0.0f;
float rate = 1.0f / f_LerpSpeedRed;
float f_CurrentTime = _CorrectionCurves.redChannel.keys[1].time;
float f_CurrentValue = _CorrectionCurves.redChannel.keys[1].value;
v2_OriginalRedKey.x = f_CurrentTime;
v2_OriginalRedKey.y = f_CurrentValue;
float f_KeyTime;
float f_KeyValue;
while (i < 1.0f)
{
i += Time.deltaTime * rate;
f_KeyTime = Mathf.Lerp(f_CurrentTime, 0.6f, i);
f_KeyValue = Mathf.Lerp(f_CurrentValue, 0.3f, i);
_CorrectionCurves.redChannel.MoveKey(1, new Keyframe(f_KeyTime, f_KeyValue));
_CorrectionCurves.redChannel.SmoothTangents(1, 1);
_CorrectionCurves.UpdateParameters();
yield return null;
}
yield return new WaitForSeconds(f_WaitToLerpBack);
i = 0.0f;
float rate2 = 1.0f / f_LerpSpeedRedBack;
float f_CurrentTime2 = _CorrectionCurves.redChannel.keys[1].time;
float f_CurrentValue2 = _CorrectionCurves.redChannel.keys[1].value;
while (i < 1.0f)
{
i += Time.deltaTime * rate2;
f_KeyTime = Mathf.Lerp(f_CurrentTime2, v2_OriginalRedKey.x, i);
f_KeyValue = Mathf.Lerp(f_CurrentValue2, v2_OriginalRedKey.y, i);
_CorrectionCurves.redChannel.MoveKey(1, new Keyframe(f_KeyTime, f_KeyValue));
_CorrectionCurves.redChannel.SmoothTangents(1, 1);
_CorrectionCurves.UpdateParameters();
yield return null;
}
}
}
Which lerps 2 values in order to make the change smooth, waits, and lerps those values back. But the problem is, curves get, lets say, broken. I can't maintain the shape, here is the visual of the curve after the first while loop and after the second one :
It seems like something to do with my tries to maintain the things by using SmoothTangent, but I've tried:
SmoothTangent(1,0);
SmoothTangent(1,0.5f);
SmoothTangent(1,1);
I've got slightly different results but none of them helped me solve the issue. So, what am I missing? Thanks in advance :)
Your answer
Follow this Question
Related Questions
Unity animation identical to Cocos2D animation not smooth? 1 Answer
(2D) How do i create a smooth movement transition from a point to another point on an orbit? 1 Answer
smooth transform look at with space self rotation 0 Answers
Why is my object not rotating smoothly ? C# 1 Answer
Problem of creating smooth animations 3 Answers