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.
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.
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
}
}
}
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");
}
}
I’ve created a class called PlayerHealth.cs and attached it to both players.
public class PlayerHealth : MonoBehaviour
{
[SerializeField] private GameObject deathParticleVFXPrefab;
private void OnTriggerEnter(Collider other)
{
Spikes spikes = other.GetComponent<Spikes>();
if (spikes)
{
Instantiate(deathParticleVFXPrefab, this.transform.position, Quaternion.Euler(0,0,90f));
StartCoroutine(RestartGameRoutine());
}
}
private IEnumerator RestartGameRoutine()
{
DisablePlayer();
yield return new WaitForSeconds(3f);
GameHandler.instance.RestartGame();
}
private void DisablePlayer()
{
GetComponent<MeshRenderer>().enabled = false;
GetComponent<PlayerMovement>().enabled = false;
}
}
I’ve also made GameHandler.cs a Singleton class so I can call it without any problem from PlayerHealth.cs
I’ve added an empty class called Spikes.cs to obstacles.