Skip to content

Instantly share code, notes, and snippets.

@GuilleUCM
Created February 13, 2015 08:31
Show Gist options
  • Save GuilleUCM/7492af227993c66bfa6f to your computer and use it in GitHub Desktop.
Save GuilleUCM/7492af227993c66bfa6f to your computer and use it in GitHub Desktop.
Unity:Animation:Scale
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