Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class TreeSeed : MonoBehaviour
- {
- public Color inheritedColor;
- public Color myColor;
- public float colorDelta;
- bool isGrowing = false;
- public float spawnTimeLow;
- public float spawnTimeHigh;
- //public float SplitTiming;
- public float DestroyMinRange;
- public float DestroyMaxRange;
- public float waitTime;
- public float maxSize;
- public float growFactor;
- //float lifeTime;
- //float t = 0f;
- public AnimationCurve curve;
- public int splitNum = 0;
- public int splitMax; //maximum number of splits that can ever happen
- public GameObject prefab;
- //MeshRenderer mr;
- // Use this for initialization
- //void OnEnable() {
- //lifeTime = Random.Range (DestroyMinRange, DestroyMaxRange);
- //t = .5f;
- //}
- void OnEnable()
- {
- myColor = new Color(inheritedColor.r + Random.Range(-colorDelta, colorDelta), inheritedColor.g + Random.Range(-colorDelta, colorDelta), inheritedColor.b + Random.Range(-colorDelta, colorDelta), inheritedColor.a);
- GetComponent<MeshRenderer>().material.color = myColor;
- StartCoroutine("SeedOut");
- StartCoroutine("Scale");
- transform.localScale = Vector3.one;
- Destroy(gameObject, Random.Range(DestroyMinRange, DestroyMaxRange));
- }
- // Update is called once per frame
- void FixedUpdate()
- {
- }
- public IEnumerator SeedOut()
- {
- while (splitNum < splitMax)
- {
- transform.localScale += new Vector3(0.1F, 0.1F, 0.1F);
- yield return new WaitForSeconds(Random.Range(spawnTimeLow, spawnTimeHigh));
- float x = curve.Evaluate(Time.timeSinceLevelLoad);
- float y = curve.Evaluate(Time.timeSinceLevelLoad);
- float z = curve.Evaluate(Time.timeSinceLevelLoad);
- GameObject obj = (GameObject)Instantiate(prefab, transform.position, Quaternion.identity);
- GameObject obj2 = (GameObject)Instantiate(prefab, transform.position, Quaternion.identity);
- splitNum++;
- GetComponent<TreeSeed>().inheritedColor = myColor;
- obj.GetComponent<TreeSeed>().splitNum = splitNum;
- } }
- public IEnumerator Scale()
- {
- float timer = 0;
- while (true) // this could also be a condition indicating "alive or dead"
- {
- // we scale all axis, so they will have the same value,
- // so we can work with a float instead of comparing vectors
- while (maxSize > transform.localScale.x)
- {
- timer += Time.deltaTime;
- transform.localScale += new Vector3(.1F, .5F, .1F) * Time.deltaTime * growFactor;
- yield return null;
- }
- // reset the timer
- yield return new WaitForSeconds(waitTime);
- timer = 0;
- while (1 < transform.localScale.x)
- {
- timer += Time.deltaTime;
- transform.localScale -= new Vector3(1, 1, 1) * Time.deltaTime * growFactor;
- yield return null;
- }
- timer = 0;
- yield return new WaitForSeconds(waitTime);
- }
- //if (isGrowing)
- //transform.localScale = new Vector3 ((.1f) + transform.localScale.x, (.1f) + transform.localScale.y, (.1f) + transform.localScale.z);
- //else
- //transform.localScale = new Vector3 ((-.1f) + transform.localScale.x, (-.1f) + transform.localScale.y, (-.1f) + transform.localScale.z);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement