Last active
November 19, 2018 14:27
-
-
Save jordyhenry/78d08089e0b7ab0b9276b9165461f839 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using UnityEngine; | |
public class CurveGrowUp : MonoBehaviour | |
{ | |
Coroutine coroutine; | |
//First keyframe of the animation | |
public Vector3 startScale; | |
//Last keyframe of the animation | |
public Vector3 endScale; | |
//Animation duration | |
public float animationTime = 2f; | |
//Animation curve | |
public AnimationCurve curve; | |
private void Update() | |
{ | |
if (Input.GetKeyDown(KeyCode.Space)) | |
{ | |
if (coroutine != null) | |
StopCoroutine(coroutine); | |
coroutine = StartCoroutine(IEScale()); | |
} | |
} | |
private IEnumerator IEScale() | |
{ | |
float t = 0; | |
do | |
{ | |
//Calculation the current time percentage in a 0 to 1 scale | |
float percent = t / animationTime; | |
//Using that percent to find a correspondent value in our curve | |
float curveValue = curve.Evaluate(percent); | |
//Using the value of the curve (0 to 1) in our lerp | |
transform.localScale = Vector3.Lerp(startScale, endScale, curveValue); | |
t += Time.deltaTime; | |
//Wait the next frame to run the next loop | |
yield return null; | |
} while (t <= animationTime); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment