Gameplay.FloorDetector - robblofield/TomeboundDocs GitHub Wiki
Gameplay.FloorDetector
Author: Shahrzad Beevis
Last Updated: 14/05/2024
Overview
A simple script, which keeps track of which floor the player character is on.
Dependencies
N/A
Contents
- Use case
- Breakdown of code
- Using Statements
- Class Declaration
- Variables
- Method/Update()
- Future Expansion
- Full Code Reference
Use case
The FloorDetector script is used for scenarios in which it is necessary to track the floor the player is on. It accurately tracks the player's position on these floors providing feedback through debugging messages on screen.
Breakdown of Code
Using Statements
using TMPro;
using UnityEngine;
Class Declaration
public class FloorDetector : MonoBehaviour
Variables
public bool isOnFloor1;
public bool isOnFloor2;
public bool isOnFloor3;
private bool wasOnFloor1;
private bool wasOnFloor2;
private bool wasOnFloor3;
public TextMeshProUGUI floorText;
public bool isOnFloor1, public bool isOnFloor2 & public bool isOnFloor3:
Booleans indicating whether the GameObject is currently on Floor 1, Floor 2, or Floor 3.
public bool wasOnFloor1, public bool wasOnFloor2 & public bool wasOnFloor3:
Booleans storing the previous states of the GameObject's position on each floor.
public TextMeshProUGUI floorText:
Reference to a TextMeshProUGUI component to display the current floor information on screen for debugging purposes.
Update()
void Update()
{
float playerX = transform.position.x;
wasOnFloor1 = isOnFloor1;
wasOnFloor2 = isOnFloor2;
wasOnFloor3 = isOnFloor3;
if (playerX >= -10 && playerX <= -6)
{
isOnFloor1 = true;
isOnFloor2 = false;
isOnFloor3 = false;
}
else if (playerX >= -2 && playerX <= 2)
{
isOnFloor1 = false;
isOnFloor2 = true;
isOnFloor3 = false;
}
else if (playerX >= 5 && playerX <= 10)
{
isOnFloor1 = false;
isOnFloor2 = false;
isOnFloor3 = true;
}
else
{
isOnFloor1 = false;
isOnFloor2 = false;
isOnFloor3 = false;
}
if (wasOnFloor1 != isOnFloor1)
{
Debug.Log(gameObject.name + (isOnFloor1 ? " is now on Floor 1" : " left Floor 1"));
}
if (wasOnFloor2 != isOnFloor2)
{
Debug.Log(gameObject.name + (isOnFloor2 ? " is now on Floor 2" : " left Floor 2"));
}
if (wasOnFloor3 != isOnFloor3)
{
Debug.Log(gameObject.name + (isOnFloor3 ? " is now on Floor 3" : " left Floor 3"));
}
if (floorText != null)
{
string currentFloor = isOnFloor1 ? "Floor 1" : (isOnFloor2 ? "Floor 2" : (isOnFloor3 ? "Floor 3" : "Unknown"));
floorText.text = "Is on " + currentFloor;
}
}
This method, executed every frame, calculates the player's position along the x-axis and updates the isOnFloor variables accordingly, marking the player's presence on different floors within specific x-coordinate ranges. It monitors changes in the player's floor position compared to the previous frame, logging messages to indicate whether the player has entered or left a floor. Additionally, if the floorText component is assigned, it dynamically updates the displayed text to reflect the current floor.
Future Expansion
N/A
Full Code Reference
using TMPro;
using UnityEngine;
public class FloorDetector : MonoBehaviour
{
public bool isOnFloor1;
public bool isOnFloor2;
public bool isOnFloor3;
private bool wasOnFloor1;
private bool wasOnFloor2;
private bool wasOnFloor3;
public TextMeshProUGUI floorText;
void Update()
{
float playerX = transform.position.x;
wasOnFloor1 = isOnFloor1;
wasOnFloor2 = isOnFloor2;
wasOnFloor3 = isOnFloor3;
if (playerX >= -10 && playerX <= -6)
{
isOnFloor1 = true;
isOnFloor2 = false;
isOnFloor3 = false;
}
else if (playerX >= -2 && playerX <= 2)
{
isOnFloor1 = false;
isOnFloor2 = true;
isOnFloor3 = false;
}
else if (playerX >= 5 && playerX <= 10)
{
isOnFloor1 = false;
isOnFloor2 = false;
isOnFloor3 = true;
}
else
{
isOnFloor1 = false;
isOnFloor2 = false;
isOnFloor3 = false;
}
if (wasOnFloor1 != isOnFloor1)
{
Debug.Log(gameObject.name + (isOnFloor1 ? " is now on Floor 1" : " left Floor 1"));
}
if (wasOnFloor2 != isOnFloor2)
{
Debug.Log(gameObject.name + (isOnFloor2 ? " is now on Floor 2" : " left Floor 2"));
}
if (wasOnFloor3 != isOnFloor3)
{
Debug.Log(gameObject.name + (isOnFloor3 ? " is now on Floor 3" : " left Floor 3"));
}
if (floorText != null)
{
string currentFloor = isOnFloor1 ? "Floor 1" : (isOnFloor2 ? "Floor 2" : (isOnFloor3 ? "Floor 3" : "Unknown"));
floorText.text = "Is on " + currentFloor;
}
}
}