using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class Player_Health_Script : MonoBehaviour { public int fatalY = -10; public int playerLayer = 8; private float width; private float height; private GameObject healthDisplay; private GameObject lifesDisplay; private bool dying; private int hurtCooldown = 0; private AudioManager_Script audioManager; void Start() { width = GetComponent().bounds.size.x; height = GetComponent().bounds.size.y; healthDisplay = GameObject.FindGameObjectWithTag("HealthDisplay"); lifesDisplay = GameObject.FindGameObjectWithTag("LifesDisplay"); audioManager = GameObject.FindGameObjectWithTag("AudioManager").GetComponent(); } void Update() { gameObject.GetComponent().SetBool("hurt", false); Color oldColor = gameObject.GetComponent().color; if (oldColor != null && (oldColor.r < 1 || oldColor.g < 1 || oldColor.b < 1)) { gameObject.GetComponent().color = new Color(oldColor.r / 0.6f, oldColor.r / 0.6f, oldColor.r / 0.6f, 1); } if (dying) return; if (hurtCooldown > 0) hurtCooldown--; int layerMask = ~(1 << playerLayer); for (int x = -1; x <= 1; x += 2) { for (float y = gameObject.transform.position.y - height * 0.4f; y <= gameObject.transform.position.y + height * 0.4f; y += 0.1f) { RaycastHit2D hit = Physics2D.Raycast(new Vector2(gameObject.transform.position.x, y), new Vector2(x, 0), width * 0.6f, layerMask); if (hit.collider && (hit.collider.tag == "Enemy" || hit.collider.tag == "Projectile")) { gameObject.GetComponent().AddForce(new Vector2(-x * 1200, 200)); hit.collider.GetComponent().AddForce(new Vector2(x * 1200, 200)); if (hurtCooldown <= 0) hurtPlayer(); if (hit.collider.tag == "Projectile") { DestroyImmediate(hit.collider.gameObject); } break; } } } if (gameObject.transform.position.y <= fatalY) Static_GlobalStats.health = 0; if (Static_GlobalStats.health <= 0) { int randomDir = Random.Range(0f, 1f) > 0.5f ? -1 : 1; gameObject.GetComponent().constraints = RigidbodyConstraints2D.None; gameObject.GetComponent().AddForceAtPosition(new Vector2(randomDir * 40, 5), new Vector2(width, height)); Static_GlobalStats.lifes--; if (Static_GlobalStats.lifes > 0) { StartCoroutine(restartLevel()); } else { StartCoroutine(gameOver()); } audioManager.playSfx("death"); audioManager.playComment("death"); dying = true; } if (healthDisplay) { healthDisplay.gameObject.GetComponent().text = "" + Static_GlobalStats.health + "%"; float b = (float)Static_GlobalStats.health / 100; float g = 0.5f + b / 2; healthDisplay.gameObject.GetComponent().color = new Color(1, g, b, 1); } if (lifesDisplay) lifesDisplay.gameObject.GetComponent().text = "" + Static_GlobalStats.lifes; gameObject.GetComponent().SetFloat("health", Static_GlobalStats.health); } void hurtPlayer() { hurtCooldown += 20; Static_GlobalStats.health = Mathf.Clamp(Static_GlobalStats.health - 15, 0, 100); if (audioManager) { if (Static_GlobalStats.health > 0) audioManager.playComment("hurt"); } gameObject.GetComponent().SetBool("hurt", true); gameObject.GetComponent().color = new Color(1, 0.5f, 0.3f, 1); } IEnumerator restartLevel() { yield return new WaitForSeconds(3); Static_GlobalStats.health = 100; SceneManager.LoadScene(SceneManager.GetActiveScene().name); } IEnumerator gameOver() { yield return new WaitForSeconds(3); Static_GlobalStats.totalScore = 0; Static_GlobalStats.levelScore = 0; Static_GlobalStats.bauteile = 0; Static_GlobalStats.bauteileLevel = 0; Static_GlobalStats.lifes = 3; Static_GlobalStats.health = 100; SceneManager.LoadScene("Menu_GameOver"); } }