Created
February 13, 2015 08:31
-
-
Save GuilleUCM/7492af227993c66bfa6f to your computer and use it in GitHub Desktop.
Unity:Animation:Scale
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 UnityEngine; | |
using System.Collections; | |
/// <summary> | |
/// Scales proportionally a gameobject over the X and Z axis | |
/// </summary> | |
public class Scale : MonoBehaviour { | |
/// <summary> | |
/// The expected final scale (1.0f is the current scale) | |
/// </summary> | |
public float finalScale =0.0f; // X-Z | |
/// <summary> | |
/// Total time the gameobject will spend to scale | |
/// </summary> | |
public float animationTime = 1.0f; | |
/// <summary> | |
/// If reverse is selected, the gameobject will start at the final scale and it will change until it achieves its current scale | |
/// </summary> | |
public bool reverse; | |
private float diff ; | |
private float startTime; | |
private Vector3 startVec; | |
private Vector3 finalVec; | |
/// <summary> | |
/// Initialization when enabled | |
/// </summary> | |
void OnEnable () { | |
startTime = Time.time; | |
startVec = gameObject.transform.localScale; | |
finalVec = gameObject.transform.localScale * finalScale; | |
finalVec.y = gameObject.transform.localScale.y; | |
if (reverse) { | |
Vector3 temp = startVec; | |
startVec = finalVec; | |
finalVec = temp; | |
transform.localScale = finalVec; | |
} | |
} | |
// Update is called once per frame | |
void Update () { | |
float timeCovered = (Time.time - startTime); | |
float fracTime = timeCovered / animationTime; | |
transform.localScale = Vector3.Lerp(startVec, finalVec, fracTime); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment