You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.5 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. public class confettiController : MonoBehaviour {
  4. public float lifeTime = 0.5f;
  5. public float speed = 5;
  6. private float startTime;
  7. private float startScale;
  8. // Use this for initialization
  9. void Start () {
  10. float rand = Random.Range (0, 7);
  11. int randColour = Mathf.FloorToInt (rand);
  12. startTime = Time.time;
  13. startScale = transform.localScale.x;
  14. rigidbody2D.velocity = Random.onUnitSphere * speed;
  15. switch (randColour) {
  16. case 0:
  17. renderer.material.color = Color.cyan;
  18. break;
  19. case 1:
  20. renderer.material.color = Color.red;
  21. break;
  22. case 2:
  23. renderer.material.color = Color.blue;
  24. break;
  25. case 3:
  26. renderer.material.color = Color.green;
  27. break;
  28. case 4:
  29. renderer.material.color = Color.yellow;
  30. break;
  31. case 5:
  32. renderer.material.color = Color.magenta;
  33. break;
  34. default:
  35. renderer.material.color = Color.black;
  36. break;
  37. }
  38. }
  39. // Update is called once per frame
  40. void Update () {
  41. float newScale = Mathf.Lerp(0, startScale, Time.deltaTime / lifeTime);
  42. transform.localScale = new Vector3(startScale-newScale, startScale-newScale, startScale-newScale);
  43. if(Time.time - startTime > lifeTime) {
  44. Destroy (gameObject, 0.0f);
  45. }
  46. }
  47. void OnCollisionEnter2D(Collision2D col){
  48. if (col.collider.tag == "ground" && Time.time - startTime > 2) {
  49. gameObject.GetComponent<BoxCollider2D> ().enabled = false;
  50. gameObject.GetComponent<Rigidbody2D> ().isKinematic = true;
  51. }
  52. }
  53. }