DOUBLE ROOM QUEST: ‘Floor Is Lava’ - Solutions

Quest: Double Room Quest
Challenge: Floor Is Lava

Feel free to share your solutions, ideas and creations below. If you get stuck, you can find some ideas here for completing this challenge.

DOUBLE ROOM QUEST ‘Floor Is Lava’ Small

GameHandler.cs

public class GameHandler : MonoBehaviour
{
    [SerializeField] private GameObject youWinPanel = null;
    [SerializeField] private GameObject youLosePanel = null; // Floor Is Lava Challenge 

    [SerializeField] private List<PlayerMovement> allPlayerCubes = new List<PlayerMovement>();

    private void Start()
    {
        allPlayerCubes.AddRange(FindObjectsOfType<PlayerMovement>());
        youWinPanel.SetActive(false);
        youLosePanel.SetActive(false); // Floor Is Lava Challenge 
    }

    public void RemovePlayerCubeFromList(PlayerMovement thisCube)
    {
        allPlayerCubes.Remove(thisCube);
        CheckIfLevelComplete();
    }

    private void CheckIfLevelComplete()
    {
        if (allPlayerCubes.Count <= 0)
        {
            youWinPanel.SetActive(true);
        }
    }

    // Floor Is Lava Challenge 
    public void CheckIfPlayerHitSpike()
    {
        for (int i = 0; i < allPlayerCubes.Count; i++)
        {
            if(allPlayerCubes[i].IsGameOver == true)
            {
                youLosePanel.SetActive(true);
                Time.timeScale = 0;
            }
        }
    }

    public void RestartGame()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
        Time.timeScale = 1;
    }
}

PlayerMovement.cs

public class PlayerMovement : MonoBehaviour
{
    [SerializeField] float moveSpeed;
    [SerializeField] Rigidbody rb;
    [SerializeField] GameHandler gameHandler;

    private Vector3 moveDirection;

    private bool isGameOver = false;
    public bool IsGameOver
    {
        get
        {
            return isGameOver;
        }
        private set
        {
            isGameOver = value;
        }
    }

    void Update()
    {
        ProcessInputs();
    }

    private void FixedUpdate()
    {
        Move();
        Debug.Log(IsGameOver);
    }

    private void ProcessInputs()
    {
        float moveX = Input.GetAxisRaw("Horizontal");
        float moveZ = Input.GetAxisRaw("Vertical");

        moveDirection = new Vector3(moveX, 0f, moveZ).normalized;
}

    private void Move()
    {
        rb.velocity = new Vector3(moveDirection.x * moveSpeed, 0, moveDirection.z * moveSpeed) * Time.deltaTime;
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Spike"))
        {
            Destroy(this.gameObject, 0.4f);
            IsGameOver = true;
            gameHandler.CheckIfPlayerHitSpike(); // Floor Is Lava Challenge 
        }
    }
}
1 Like

Since this new script’s most obvious name is Death.cs, I thought to keep it simple. But then realized that GameDev would never give us a particle named “BloodSplatter” without us wanting to use it. … Only difficulty was flipping the BloodSplatter 90 degrees on its Z axis so that the effect splattered upward and outward.

using UnityEngine.SceneManagement;

public class Death : MonoBehaviour
{
    [SerializeField] ParticleSystem bloodyCubeDestruction;

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.GetComponent<PlayerMovement>())
        {
            Destroy(collision.gameObject);
            Instantiate(bloodyCubeDestruction, collision.transform.position, Quaternion.Euler(0,0,90) );

            StartCoroutine(ResetLevel());
        }
    }

    IEnumerator ResetLevel()
    {
        yield return new WaitForSeconds(2f);
        SceneManager.LoadScene("Level1");
    }
}