game(scripts): commented bedroomtask & fixing infocollector
This commit is contained in:
parent
ddcda12d74
commit
be8317253f
2 changed files with 92 additions and 20 deletions
|
@ -13,95 +13,146 @@ using UnityEngine.XR.Interaction.Toolkit;
|
||||||
|
|
||||||
public class BedroomTask : MonoBehaviour
|
public class BedroomTask : MonoBehaviour
|
||||||
{
|
{
|
||||||
|
// To track how much trash has been collected so far
|
||||||
public int trashCollected = 0;
|
public int trashCollected = 0;
|
||||||
|
|
||||||
|
// Defines how much trash is needed to collect in order to unlock the door
|
||||||
public int trashRequired = 10;
|
public int trashRequired = 10;
|
||||||
public GameObject door; // The VR swingable door object
|
|
||||||
|
// Defines the door
|
||||||
|
public GameObject door;
|
||||||
private XRGrabInteractable grabInteractable;
|
private XRGrabInteractable grabInteractable;
|
||||||
|
|
||||||
|
// Defines door handle
|
||||||
private Collider doorCollider;
|
private Collider doorCollider;
|
||||||
|
|
||||||
|
// Defines door's rigidbody to lock it
|
||||||
private Rigidbody doorRigidbody;
|
private Rigidbody doorRigidbody;
|
||||||
|
|
||||||
|
// Defines UI references
|
||||||
public GameObject lockedDoorUI;
|
public GameObject lockedDoorUI;
|
||||||
public GameObject unlockedDoorUI;
|
public GameObject unlockedDoorUI;
|
||||||
|
|
||||||
void Start()
|
void Start()
|
||||||
{
|
{
|
||||||
|
// Hide all UI prompts on start
|
||||||
lockedDoorUI.SetActive(false);
|
lockedDoorUI.SetActive(false);
|
||||||
unlockedDoorUI.SetActive(false);
|
unlockedDoorUI.SetActive(false);
|
||||||
|
|
||||||
|
// Ensure door is not null
|
||||||
if (door != null)
|
if (door != null)
|
||||||
{
|
{
|
||||||
|
// Get the components of the door
|
||||||
grabInteractable = door.GetComponent<XRGrabInteractable>();
|
grabInteractable = door.GetComponent<XRGrabInteractable>();
|
||||||
doorCollider = door.GetComponent<Collider>();
|
doorCollider = door.GetComponent<Collider>();
|
||||||
doorRigidbody = door.GetComponent<Rigidbody>();
|
doorRigidbody = door.GetComponent<Rigidbody>();
|
||||||
|
|
||||||
|
// Ensure grab interactable is not null
|
||||||
if (grabInteractable != null)
|
if (grabInteractable != null)
|
||||||
{
|
{
|
||||||
|
// Detect when the door is grabbed
|
||||||
grabInteractable.selectEntered.AddListener(OnDoorGrabAttempt);
|
grabInteractable.selectEntered.AddListener(OnDoorGrabAttempt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ensure rigidbody is not null
|
||||||
if (doorRigidbody != null)
|
if (doorRigidbody != null)
|
||||||
{
|
{
|
||||||
|
// Lock the door on start
|
||||||
LockDoor();
|
LockDoor();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Functions when trash is collected/thrown
|
||||||
public void CollectTrash()
|
public void CollectTrash()
|
||||||
{
|
{
|
||||||
|
// Add to trash count
|
||||||
trashCollected++;
|
trashCollected++;
|
||||||
Debug.Log($"🗑️ Trash collected: {trashCollected}/{trashRequired}");
|
Debug.Log($"Trash collected: {trashCollected}/{trashRequired}");
|
||||||
|
|
||||||
|
// If player has collected/thrown required amount of trash
|
||||||
if (trashCollected >= trashRequired)
|
if (trashCollected >= trashRequired)
|
||||||
{
|
{
|
||||||
|
// Call unlocking door function
|
||||||
UnlockDoor();
|
UnlockDoor();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Functions when door is locked
|
||||||
private void LockDoor()
|
private void LockDoor()
|
||||||
{
|
{
|
||||||
|
// Ensure rigidbody is not null
|
||||||
if (doorRigidbody != null)
|
if (doorRigidbody != null)
|
||||||
{
|
{
|
||||||
doorRigidbody.isKinematic = true; // Stops movement
|
// Stops door movement
|
||||||
doorRigidbody.constraints = RigidbodyConstraints.FreezeAll; // Completely freezes door
|
doorRigidbody.isKinematic = true;
|
||||||
|
|
||||||
|
// Freezes the door to ensure door doesn't open
|
||||||
|
doorRigidbody.constraints = RigidbodyConstraints.FreezeAll;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ensure collider is not null
|
||||||
if (doorCollider != null)
|
if (doorCollider != null)
|
||||||
{
|
{
|
||||||
doorCollider.enabled = false; // Prevents grabbing
|
// Prevents player from grabbing door handle
|
||||||
|
doorCollider.enabled = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Functions when door is unlocked
|
||||||
private void UnlockDoor()
|
private void UnlockDoor()
|
||||||
{
|
{
|
||||||
|
// Ensure rigidbody is not null
|
||||||
if (doorRigidbody != null)
|
if (doorRigidbody != null)
|
||||||
{
|
{
|
||||||
doorRigidbody.isKinematic = false; // Allows movement
|
// Allows door movement
|
||||||
|
doorRigidbody.isKinematic = false;
|
||||||
|
|
||||||
|
// Unfreezes door to let it open
|
||||||
doorRigidbody.constraints = RigidbodyConstraints.None; // Removes restrictions
|
doorRigidbody.constraints = RigidbodyConstraints.None; // Removes restrictions
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ensure collider is not null
|
||||||
if (doorCollider != null)
|
if (doorCollider != null)
|
||||||
{
|
{
|
||||||
doorCollider.enabled = true; // Enables grabbing
|
// Allows player to grab the door handle
|
||||||
|
doorCollider.enabled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Show the unlocked door UI
|
||||||
|
unlockedDoorUI.SetActive(true);
|
||||||
|
|
||||||
|
// Call the function to hide the UI after delay
|
||||||
StartCoroutine(HideMessageAfterSeconds(unlockedDoorUI, 5f));
|
StartCoroutine(HideMessageAfterSeconds(unlockedDoorUI, 5f));
|
||||||
Debug.Log("Room is clean! The door is now unlocked.");
|
Debug.Log("Room is clean! The door is now unlocked.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function when player tries to grab the door
|
||||||
private void OnDoorGrabAttempt(SelectEnterEventArgs args)
|
private void OnDoorGrabAttempt(SelectEnterEventArgs args)
|
||||||
{
|
{
|
||||||
|
// If the amount of trash collected is lesser than the required amount
|
||||||
if (trashCollected < trashRequired)
|
if (trashCollected < trashRequired)
|
||||||
{
|
{
|
||||||
|
// Show the locked door UI
|
||||||
|
lockedDoorUI.SetActive(true);
|
||||||
|
|
||||||
|
// Call the function to hide the UI after delay
|
||||||
StartCoroutine(HideMessageAfterSeconds(lockedDoorUI, 5f));
|
StartCoroutine(HideMessageAfterSeconds(lockedDoorUI, 5f));
|
||||||
Debug.Log("The door is locked! Clean the room first.");
|
Debug.Log("The door is locked! Clean the room first.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function when the task is completed
|
||||||
public bool IsTaskCompleted()
|
public bool IsTaskCompleted()
|
||||||
{
|
{
|
||||||
|
// Trash collected is more or equal to trash required to collect
|
||||||
return trashCollected >= trashRequired;
|
return trashCollected >= trashRequired;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function to hide the UI after a delay
|
||||||
private IEnumerator HideMessageAfterSeconds(GameObject uiElement, float delay)
|
private IEnumerator HideMessageAfterSeconds(GameObject uiElement, float delay)
|
||||||
{
|
{
|
||||||
|
// Waits for delay to end and hides the UI
|
||||||
yield return new WaitForSeconds(delay);
|
yield return new WaitForSeconds(delay);
|
||||||
uiElement.SetActive(false);
|
uiElement.SetActive(false);
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,10 +11,15 @@ using TMPro;
|
||||||
|
|
||||||
public class InfoCollector : MonoBehaviour
|
public class InfoCollector : MonoBehaviour
|
||||||
{
|
{
|
||||||
public float detectionRange = 5f;
|
public float detectionRange = 2f; // Adjust for accurate VR detection
|
||||||
public TMP_Text infoText; // Reference to a UI Text (TextMeshPro)
|
public float gazeTimeRequired = 2f; // Time the player must look at the object before collecting info
|
||||||
|
public float displayTime = 5f; // How long the info stays on screen
|
||||||
|
public TMP_Text infoText; // UI Text (TextMeshPro)
|
||||||
|
|
||||||
private Camera vrCamera;
|
private Camera vrCamera;
|
||||||
private bool isDisplaying = false;
|
private bool isDisplaying = false;
|
||||||
|
private GameObject currentObject = null;
|
||||||
|
private float gazeTimer = 0f;
|
||||||
|
|
||||||
void Start()
|
void Start()
|
||||||
{
|
{
|
||||||
|
@ -29,11 +34,30 @@ public class InfoCollector : MonoBehaviour
|
||||||
|
|
||||||
if (Physics.Raycast(ray, out hit, detectionRange))
|
if (Physics.Raycast(ray, out hit, detectionRange))
|
||||||
{
|
{
|
||||||
if (hit.collider.CompareTag("InfoObject") && !isDisplaying)
|
if (hit.collider.CompareTag("InfoObject"))
|
||||||
{
|
{
|
||||||
CollectInfo(hit.collider.gameObject);
|
if (currentObject == hit.collider.gameObject)
|
||||||
|
{
|
||||||
|
gazeTimer += Time.deltaTime;
|
||||||
|
if (gazeTimer >= gazeTimeRequired && !isDisplaying)
|
||||||
|
{
|
||||||
|
CollectInfo(currentObject);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Reset timer if looking at a new object
|
||||||
|
currentObject = hit.collider.gameObject;
|
||||||
|
gazeTimer = 0f;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Reset if no valid object is in view
|
||||||
|
currentObject = null;
|
||||||
|
gazeTimer = 0f;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CollectInfo(GameObject obj)
|
void CollectInfo(GameObject obj)
|
||||||
|
@ -42,8 +66,8 @@ public class InfoCollector : MonoBehaviour
|
||||||
infoText.text = "Info Collected: " + obj.name + "\n" + GetObjectInfo(obj);
|
infoText.text = "Info Collected: " + obj.name + "\n" + GetObjectInfo(obj);
|
||||||
Debug.Log("Collected information from: " + obj.name);
|
Debug.Log("Collected information from: " + obj.name);
|
||||||
|
|
||||||
// Hide text after 3 seconds
|
// Hide text after displayTime
|
||||||
Invoke("ClearText", 3f);
|
Invoke(nameof(ClearText), displayTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClearText()
|
void ClearText()
|
||||||
|
@ -55,12 +79,9 @@ public class InfoCollector : MonoBehaviour
|
||||||
string GetObjectInfo(GameObject obj)
|
string GetObjectInfo(GameObject obj)
|
||||||
{
|
{
|
||||||
// Define object descriptions here
|
// Define object descriptions here
|
||||||
if (obj.name == "AncientArtifact")
|
if (obj.name == "Info")
|
||||||
return "This artifact was used by an old civilization...";
|
return "Test";
|
||||||
|
|
||||||
if (obj.name == "HologramTerminal")
|
|
||||||
return "A futuristic data console storing historical records.";
|
|
||||||
|
|
||||||
return "An unknown object with mysterious origins.";
|
return "An unknown object with mysterious origins.";
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue