Newer
Older
fall / FallUnity / Assets / Game / LeafSpawner.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LeafSpawner : MonoBehaviour {
    public float Radius = 3;
    public float Rate = 5;
    public LeafPhysics[] Leafs;
    // Use this for initialization
    private LeafHandler leafHandler;
	void Start () {
        leafHandler = GameObject.FindObjectOfType<LeafHandler>();
	}

    private List<LeafPhysics> leaves = new List<LeafPhysics>();

    private float time = 0;
	
	// Update is called once per frame
	void Update () {
        float interval = 1 / Rate;
        time += Time.deltaTime;
        while (time > interval)
        {
            if (leafHandler != null && leafHandler.NumLeaves < leafHandler.MaxLeaves)
            {
                time -= interval;
                Vector2 offset = Random.insideUnitCircle;
                Vector3 point = transform.position + new Vector3(offset.x * Radius, 0, offset.y * Radius);
                LeafPhysics leaf = GameObject.Instantiate<LeafPhysics>(Leafs[Random.Range(0, Leafs.Length)]);
                leaves.Add(leaf);
                leaf.transform.position = point;
                leaf.transform.rotation = Quaternion.Euler(new Vector3((Random.value - 0.5f) * 25, Random.value * 360, (Random.value - 0.5f) * 25));

                float scale = Random.Range(0.9f, 1.3f);
                leaf.transform.localScale = new Vector3(scale, scale, scale);
            }
        }
	}    

}