using System.Collections;
using UnityEngine;

public class CubeBounce : MonoBehaviour
{
	float velocity;
	
	IEnumerator Start()
	{
		var block = new MaterialPropertyBlock();
		block.SetColor("_Color", Random.ColorHSV(0, 1, 1, 1, 1, 1));
		transform.GetChild(0).GetComponent<MeshRenderer>().SetPropertyBlock(block);

		while(true)
		{
			if(transform.position.y < .01f)
			{
				yield return new WaitForSeconds(Random.Range(0f, 2f));

				Bounce();
			}

			yield return null;
		}
	}

	void Update()
	{
		velocity -= Time.deltaTime * 9.81f;
		var pos = transform.position;
		pos.y += velocity * Time.deltaTime;

		if(pos.y <= 0)
		{
			pos.y = 0;
			velocity = 0;
		}

		transform.position = pos;
	}

	void Bounce()
	{
		velocity = Random.Range(1.5f, 4.5f);
	}
}
