game(scripts): commented bedroomtask & fixing infocollector

This commit is contained in:
rezazfn 2025-02-07 13:51:16 +08:00
parent ddcda12d74
commit be8317253f
2 changed files with 92 additions and 20 deletions

View file

@ -13,95 +13,146 @@ using UnityEngine.XR.Interaction.Toolkit;
public class BedroomTask : MonoBehaviour
{
// To track how much trash has been collected so far
public int trashCollected = 0;
// Defines how much trash is needed to collect in order to unlock the door
public int trashRequired = 10;
public GameObject door; // The VR swingable door object
// Defines the door
public GameObject door;
private XRGrabInteractable grabInteractable;
// Defines door handle
private Collider doorCollider;
// Defines door's rigidbody to lock it
private Rigidbody doorRigidbody;
// Defines UI references
public GameObject lockedDoorUI;
public GameObject unlockedDoorUI;
void Start()
{
// Hide all UI prompts on start
lockedDoorUI.SetActive(false);
unlockedDoorUI.SetActive(false);
// Ensure door is not null
if (door != null)
{
// Get the components of the door
grabInteractable = door.GetComponent<XRGrabInteractable>();
doorCollider = door.GetComponent<Collider>();
doorRigidbody = door.GetComponent<Rigidbody>();
// Ensure grab interactable is not null
if (grabInteractable != null)
{
// Detect when the door is grabbed
grabInteractable.selectEntered.AddListener(OnDoorGrabAttempt);
}
// Ensure rigidbody is not null
if (doorRigidbody != null)
{
// Lock the door on start
LockDoor();
}
}
}
// Functions when trash is collected/thrown
public void CollectTrash()
{
// Add to trash count
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)
{
// Call unlocking door function
UnlockDoor();
}
}
// Functions when door is locked
private void LockDoor()
{
// Ensure rigidbody is not null
if (doorRigidbody != null)
{
doorRigidbody.isKinematic = true; // Stops movement
doorRigidbody.constraints = RigidbodyConstraints.FreezeAll; // Completely freezes door
// Stops door movement
doorRigidbody.isKinematic = true;
// Freezes the door to ensure door doesn't open
doorRigidbody.constraints = RigidbodyConstraints.FreezeAll;
}
// Ensure collider is not 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()
{
// Ensure rigidbody is not 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
}
// Ensure collider is not 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));
Debug.Log("Room is clean! The door is now unlocked.");
}
// Function when player tries to grab the door
private void OnDoorGrabAttempt(SelectEnterEventArgs args)
{
// If the amount of trash collected is lesser than the required amount
if (trashCollected < trashRequired)
{
// Show the locked door UI
lockedDoorUI.SetActive(true);
// Call the function to hide the UI after delay
StartCoroutine(HideMessageAfterSeconds(lockedDoorUI, 5f));
Debug.Log("The door is locked! Clean the room first.");
}
}
// Function when the task is completed
public bool IsTaskCompleted()
{
// Trash collected is more or equal to trash required to collect
return trashCollected >= trashRequired;
}
// Function to hide the UI after a delay
private IEnumerator HideMessageAfterSeconds(GameObject uiElement, float delay)
{
// Waits for delay to end and hides the UI
yield return new WaitForSeconds(delay);
uiElement.SetActive(false);
}

View file

@ -11,10 +11,15 @@ using TMPro;
public class InfoCollector : MonoBehaviour
{
public float detectionRange = 5f;
public TMP_Text infoText; // Reference to a UI Text (TextMeshPro)
public float detectionRange = 2f; // Adjust for accurate VR detection
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 bool isDisplaying = false;
private GameObject currentObject = null;
private float gazeTimer = 0f;
void Start()
{
@ -29,11 +34,30 @@ public class InfoCollector : MonoBehaviour
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)
@ -42,8 +66,8 @@ public class InfoCollector : MonoBehaviour
infoText.text = "Info Collected: " + obj.name + "\n" + GetObjectInfo(obj);
Debug.Log("Collected information from: " + obj.name);
// Hide text after 3 seconds
Invoke("ClearText", 3f);
// Hide text after displayTime
Invoke(nameof(ClearText), displayTime);
}
void ClearText()
@ -55,11 +79,8 @@ public class InfoCollector : MonoBehaviour
string GetObjectInfo(GameObject obj)
{
// Define object descriptions here
if (obj.name == "AncientArtifact")
return "This artifact was used by an old civilization...";
if (obj.name == "HologramTerminal")
return "A futuristic data console storing historical records.";
if (obj.name == "Info")
return "Test";
return "An unknown object with mysterious origins.";
}