game(assets): added info collecting script and fixed old scripts

This commit is contained in:
rezazfn 2025-02-07 11:15:46 +08:00
parent 396d787b69
commit 1b18ab67b7
5 changed files with 172 additions and 19 deletions

View file

@ -9,24 +9,37 @@ using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.ProBuilder.Shapes; using UnityEngine.ProBuilder.Shapes;
using UnityEngine.XR.Interaction.Toolkit.Interactables; using UnityEngine.XR.Interaction.Toolkit.Interactables;
using UnityEngine.XR.Interaction.Toolkit;
public class BedroomTask : MonoBehaviour public class BedroomTask : MonoBehaviour
{ {
public int trashCollected = 0; public int trashCollected = 0;
public int trashToUnlock = 1; public int trashRequired = 10;
public GameObject door; public GameObject door; // The VR swingable door object
public XRGrabInteractable doorInteractable; private XRGrabInteractable grabInteractable;
private Collider doorCollider;
private Rigidbody doorRigidbody;
public GameObject lockedDoorUI;
public GameObject unlockedDoorUI;
// Start is called before the first frame update
void Start() void Start()
{ {
lockedDoorUI.SetActive(false);
unlockedDoorUI.SetActive(false);
if (door != null) if (door != null)
{ {
doorInteractable = door.GetComponent<XRGrabInteractable>(); grabInteractable = door.GetComponent<XRGrabInteractable>();
if (doorInteractable != null) doorCollider = door.GetComponent<Collider>();
doorRigidbody = door.GetComponent<Rigidbody>();
if (grabInteractable != null)
{ {
doorInteractable.enabled = false; grabInteractable.selectEntered.AddListener(OnDoorGrabAttempt);
Debug.Log("Room is not cleaned! Door is locked"); }
if (doorRigidbody != null)
{
LockDoor();
} }
} }
} }
@ -34,18 +47,62 @@ public class BedroomTask : MonoBehaviour
public void CollectTrash() public void CollectTrash()
{ {
trashCollected++; trashCollected++;
Debug.Log("Trash Collected: " + trashCollected + "/" + trashToUnlock); Debug.Log($"🗑️ Trash collected: {trashCollected}/{trashRequired}");
if (trashCollected >= trashToUnlock) if (trashCollected >= trashRequired)
{ {
UnlockDoor(); UnlockDoor();
Debug.Log("Cleaning task completed! Unlocking door..."); }
}
private void LockDoor()
{
if (doorRigidbody != null)
{
doorRigidbody.isKinematic = true; // Stops movement
doorRigidbody.constraints = RigidbodyConstraints.FreezeAll; // Completely freezes door
}
if (doorCollider != null)
{
doorCollider.enabled = false; // Prevents grabbing
} }
} }
private void UnlockDoor() private void UnlockDoor()
{ {
doorInteractable.enabled = true; if (doorRigidbody != null)
Debug.Log("Room is cleaned! Door unlocked"); {
doorRigidbody.isKinematic = false; // Allows movement
doorRigidbody.constraints = RigidbodyConstraints.None; // Removes restrictions
}
if (doorCollider != null)
{
doorCollider.enabled = true; // Enables grabbing
}
StartCoroutine(HideMessageAfterSeconds(unlockedDoorUI, 5f));
Debug.Log("Room is clean! The door is now unlocked.");
}
private void OnDoorGrabAttempt(SelectEnterEventArgs args)
{
if (trashCollected < trashRequired)
{
StartCoroutine(HideMessageAfterSeconds(lockedDoorUI, 5f));
Debug.Log("The door is locked! Clean the room first.");
}
}
public bool IsTaskCompleted()
{
return trashCollected >= trashRequired;
}
private IEnumerator HideMessageAfterSeconds(GameObject uiElement, float delay)
{
yield return new WaitForSeconds(delay);
uiElement.SetActive(false);
} }
} }

View file

@ -0,0 +1,66 @@
/*
Author: Reza
Date: 7/2/25
Description: Collects information when a player looks at objects long enough
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class InfoCollector : MonoBehaviour
{
public float detectionRange = 5f;
public TMP_Text infoText; // Reference to a UI Text (TextMeshPro)
private Camera vrCamera;
private bool isDisplaying = false;
void Start()
{
vrCamera = Camera.main;
infoText.text = ""; // Clear text initially
}
void Update()
{
Ray ray = new Ray(vrCamera.transform.position, vrCamera.transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, detectionRange))
{
if (hit.collider.CompareTag("InfoObject") && !isDisplaying)
{
CollectInfo(hit.collider.gameObject);
}
}
}
void CollectInfo(GameObject obj)
{
isDisplaying = true;
infoText.text = "Info Collected: " + obj.name + "\n" + GetObjectInfo(obj);
Debug.Log("Collected information from: " + obj.name);
// Hide text after 3 seconds
Invoke("ClearText", 3f);
}
void ClearText()
{
infoText.text = "";
isDisplaying = false;
}
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.";
return "An unknown object with mysterious origins.";
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6cb4698acb16cc94e8caf48cad7156c4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -12,6 +12,7 @@ using UnityEngine.UIElements;
public class Letter : MonoBehaviour public class Letter : MonoBehaviour
{ {
public GameObject letterUI; public GameObject letterUI;
public AudioClip scribble;
private void Start() private void Start()
{ {

View file

@ -12,13 +12,31 @@ public class Trash : MonoBehaviour
{ {
private BedroomTask bedroomTask; private BedroomTask bedroomTask;
private void Start()
{
// Find the BedroomTask script in the scene
bedroomTask = FindObjectOfType<BedroomTask>();
if (bedroomTask == null)
{
Debug.LogError("BedroomTask script not found in the scene!");
}
}
private void OnTriggerEnter(Collider other) private void OnTriggerEnter(Collider other)
{ {
if (other.CompareTag("TrashBin")) if (other.CompareTag("TrashBin"))
{
if (bedroomTask != null)
{ {
bedroomTask.CollectTrash(); bedroomTask.CollectTrash();
Debug.Log("Trash thrown in the bin! Count: " + bedroomTask.trashCollected); Debug.Log("🗑️ Trash thrown in the bin! Count: " + bedroomTask.trashCollected);
Destroy(gameObject); Destroy(gameObject);
} }
else
{
Debug.LogError("BedroomTask reference is missing!");
}
}
} }
} }