using System.Collections; using System.Collections.Generic; using UnityEngine; public…
- User
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Barrier : MonoBehaviour { public GameObject barrierPrefab; // Reference to the barrier prefab public float minInterval = 5f; // Minimum interval between barrier activations public float maxInterval = 10f; // Maximum interval between barrier activations
codeprivate bool isBarrierActive = false; void Start() { // Start the barrier activation loop StartCoroutine(ActivateBarrier()); } IEnumerator ActivateBarrier() { while (true) { // Check if barrier is already active if (!isBarrierActive) { // Generate a random activation delay float activationDelay = Random.Range(minInterval, maxInterval); // Waiting for the activation delay yield return new WaitForSeconds(activationDelay); // Instantiate the barrier prefab GameObject barrier = Instantiate(barrierPrefab, Vector3.zero, Quaternion.identity); barrier.transform.parent = transform; // Set the boss as the parent // Relative position of the barrier to the boss Vector3 relativePosition = new Vector3(3.27f, 0.7f, 0f); // Set the barrier position relative to the boss’s position barrier.transform.localPosition = relativePosition; // Set the barrier as active isBarrierActive = true; // Destroy the barrier after 3 seconds Destroy(barrier, 3f); // Delay for 3 seconds (duration of the barrier) yield return new WaitForSeconds(3f); // Set the barrier as inactive isBarrierActive = false; } yield return null; } } - Assistant
- User
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class BossHealth : MonoBehaviour { public int maxHealth = 5; private int currentHealth;
codeprivate ScoreCounter scoreCounter; // Reference to the ScoreCounter script private void Start() { currentHealth = maxHealth; scoreCounter = FindObjectOfType<ScoreCounter>(); // Find the ScoreCounter script in the scene } public void TakeDamage(int damageAmount) { currentHealth -= damageAmount; if (currentHealth <= 0) { Die(); } } private void Die() { // Add 300 points to the score when the boss dies scoreCounter.score += 300; // Destroy the boss object when health reaches 0 Destroy(gameObject); // Check if the current scene is "Level1" and change to "Level2" if true if (SceneManager.GetActiveScene().name.Equals("Level1")) { SceneManager.LoadScene("Level2"); } // Check if the current scene is "Level2" and change to "Level3" if true if (SceneManager.GetActiveScene().name.Equals("Level2")) { SceneManager.LoadScene("Level3"); } } - Assistant
The BossHealth script you provided handles the boss's health and its death. To make the boss invincible while the barrier is active, you can add a condition to the TakeDamage method that only allows damage to be taken when the barrier is inactive. Here's an updated version of the BossHealth script:
c# - User
update the barrier script
- Assistant