Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using Unity.VisualScripting;
- using UnityEngine;
- public class ShieldScript : MonoBehaviour
- {
- int life = 3; //number of hits
- Renderer rend; //connection to the Renderer component
- void Start()
- {
- rend = GetComponent<Renderer>();
- }
- void OnTriggerEnter2D(Collider2D other)
- {
- //If the shield touches the alien itself, it will be completely destroyed
- if (other.gameObject.tag == "Alien")
- {
- Destroy(gameObject);
- }
- //If it touches the projectile, the projectile will be destroyed and it will also reduce the durability of the shield
- if (other.gameObject.tag == "AlienBullet")
- {
- Destroy(other.gameObject);
- ChangeLife();
- }
- }
- void ChangeLife()
- {
- //We define the next states that the colors will have
- Color[] col = { Color.red, Color.green, Color.blue };
- //lower the number of hits
- life--;
- //If we go below 0
- if (life < 0)
- {
- //the shield is destroyed
- Destroy(gameObject);
- //end the method
- return;
- }
- //change the state
- rend.material.color = col[life];
- }
- }
Add Comment
Please, Sign In to add comment