SHOW:
|
|
- or go back to the newest paste.
1 | using System.Collections; | |
2 | using System.Collections.Generic; | |
3 | using Unity.VisualScripting; | |
4 | using UnityEngine; | |
5 | ||
6 | public class ShieldScript : MonoBehaviour | |
7 | { | |
8 | ||
9 | int life = 3; //number of hits | |
10 | Renderer rend; //connection to the Renderer component | |
11 | ||
12 | void Start() | |
13 | { | |
14 | rend = GetComponent<Renderer>(); | |
15 | } | |
16 | ||
17 | void OnTriggerEnter2D(Collider2D other) | |
18 | { | |
19 | //If the shield touches the alien itself, it will be completely destroyed | |
20 | if (other.gameObject.tag == "Alien") | |
21 | { | |
22 | Destroy(gameObject); | |
23 | } | |
24 | ||
25 | //If it touches the projectile, the projectile will be destroyed and it will also reduce the durability of the shield | |
26 | if (other.gameObject.tag == "AlienBullet") | |
27 | { | |
28 | Destroy(other.gameObject); | |
29 | ChangeLife(); | |
30 | } | |
31 | ||
32 | } | |
33 | ||
34 | ||
35 | void ChangeLife() | |
36 | { | |
37 | //We define the next states that the colors will have | |
38 | Color[] col = { Color.red, Color.green, Color.blue }; | |
39 | ||
40 | //lower the number of hits | |
41 | life--; | |
42 | ||
43 | //If we go below 0 | |
44 | if (life < 0) | |
45 | { | |
46 | //the shield is destroyed | |
47 | Destroy(gameObject); | |
48 | //end the method | |
49 | return; | |
50 | } | |
51 | //change the state | |
52 | rend.material.color = col[life]; | |
53 | ||
54 | } | |
55 | } |