SHOW:
|
|
- or go back to the newest paste.
| 1 | using System.Collections; | |
| 2 | using System.Collections.Generic; | |
| 3 | using UnityEngine; | |
| 4 | ||
| 5 | public class BricksScript : MonoBehaviour | |
| 6 | {
| |
| 7 | //Zmienna prywatna, którą można zmieniać w inspektorze | |
| 8 | //Ilość życia naszej cegiełki | |
| 9 | ||
| 10 | public int life = 1; | |
| 11 | ||
| 12 | ||
| 13 | //Za każdym razem jak ona się zetknie z czymś | |
| 14 | private void OnCollisionEnter(Collision collision) | |
| 15 | {
| |
| 16 | //Jeżeli ma to tag Ball | |
| 17 | if(collision.gameObject.tag == "Ball") | |
| 18 | {
| |
| 19 | //usuawamy jedno życie | |
| 20 | life--; | |
| 21 | BrickColor(); | |
| 22 | //Jeżeli życia są równe lub mniejsze od 0 | |
| 23 | if (life <= 0) | |
| 24 | {
| |
| 25 | GameManager.instance.bricks.Remove(this.gameObject); | |
| 26 | GameManager.instance.UpdateUI(); | |
| 27 | Destroy(gameObject); //Zniszcz obiekt | |
| 28 | } | |
| 29 | } | |
| 30 | } | |
| 31 | ||
| 32 | public void SetBrick(int life) | |
| 33 | {
| |
| 34 | this.life = life; | |
| 35 | if (life <= 0) Destroy(gameObject); | |
| 36 | BrickColor(); | |
| 37 | } | |
| 38 | ||
| 39 | void BrickColor() | |
| 40 | {
| |
| 41 | Renderer renderer = GetComponent<Renderer>(); | |
| 42 | Color[] colors = { Color.white, Color.blue, Color.red, Color.green };
| |
| 43 | int colorIndex = Mathf.Clamp(life - 1, 0, colors.Length-1); | |
| 44 | renderer.material.color = colors[colorIndex]; | |
| 45 | } | |
| 46 | } | |
| 47 |