2025-02-07 11:15:46 +08:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
{
|
2025-02-07 14:37:22 +08:00
|
|
|
// Defines the range to "view" the object to collect information
|
|
|
|
public float detectionRange = 2f;
|
|
|
|
|
|
|
|
// Defines how long the player must look at the object for to collect information
|
2025-02-08 22:11:47 +08:00
|
|
|
public float gazeTimeRequired = 1.5f;
|
2025-02-07 14:37:22 +08:00
|
|
|
|
|
|
|
// Defines how long the information UI will remain on screen
|
2025-02-08 22:11:47 +08:00
|
|
|
public float displayTime = 7f;
|
2025-02-07 14:37:22 +08:00
|
|
|
|
|
|
|
// Defines the UI text to display
|
|
|
|
public TMP_Text infoText;
|
|
|
|
|
|
|
|
// Defines the camera
|
2025-02-07 11:15:46 +08:00
|
|
|
private Camera vrCamera;
|
2025-02-07 14:37:22 +08:00
|
|
|
|
|
|
|
// Defines whether UI is displaying to prevent spamming
|
2025-02-07 11:15:46 +08:00
|
|
|
private bool isDisplaying = false;
|
2025-02-07 14:37:22 +08:00
|
|
|
|
|
|
|
// Defines the object the player is currently looking at
|
2025-02-07 13:51:16 +08:00
|
|
|
private GameObject currentObject = null;
|
2025-02-07 14:37:22 +08:00
|
|
|
|
|
|
|
// Tracks how long the player has been looking at the object
|
2025-02-07 13:51:16 +08:00
|
|
|
private float gazeTimer = 0f;
|
2025-02-08 22:11:47 +08:00
|
|
|
|
|
|
|
// Defines Audio References
|
|
|
|
public AudioSource audioSource;
|
|
|
|
public AudioClip scribbleSound;
|
|
|
|
|
|
|
|
// Store objects that have already been collected
|
|
|
|
private HashSet<GameObject> collectedObjects = new HashSet<GameObject>();
|
2025-02-07 11:15:46 +08:00
|
|
|
|
|
|
|
void Start()
|
|
|
|
{
|
2025-02-07 14:37:22 +08:00
|
|
|
// Assigns to player's camera
|
2025-02-07 11:15:46 +08:00
|
|
|
vrCamera = Camera.main;
|
2025-02-07 14:37:22 +08:00
|
|
|
|
|
|
|
// Clear UI text initially
|
|
|
|
infoText.text = "";
|
2025-02-07 11:15:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
{
|
2025-02-07 14:37:22 +08:00
|
|
|
// Detects the direction the player is looking at
|
2025-02-07 11:15:46 +08:00
|
|
|
Ray ray = new Ray(vrCamera.transform.position, vrCamera.transform.forward);
|
|
|
|
RaycastHit hit;
|
|
|
|
|
2025-02-07 14:37:22 +08:00
|
|
|
// Stores data of object hit in the detection range
|
2025-02-07 11:15:46 +08:00
|
|
|
if (Physics.Raycast(ray, out hit, detectionRange))
|
|
|
|
{
|
2025-02-07 14:37:22 +08:00
|
|
|
// Ensures that relevant info objects are detected
|
2025-02-07 13:51:16 +08:00
|
|
|
if (hit.collider.CompareTag("InfoObject"))
|
2025-02-07 11:15:46 +08:00
|
|
|
{
|
2025-02-08 22:11:47 +08:00
|
|
|
GameObject targetObject = hit.collider.gameObject;
|
|
|
|
|
|
|
|
// **Fix: Stop gaze timer if object has been collected**
|
|
|
|
if (collectedObjects.Contains(targetObject))
|
|
|
|
{
|
|
|
|
return; // Exit without increasing gaze time
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the player is still looking at the same object, increase gaze time
|
|
|
|
if (currentObject == targetObject)
|
2025-02-07 13:51:16 +08:00
|
|
|
{
|
|
|
|
gazeTimer += Time.deltaTime;
|
2025-02-07 14:37:22 +08:00
|
|
|
|
2025-02-08 22:11:47 +08:00
|
|
|
// If gaze time reaches required time and info is not displayed yet
|
2025-02-07 13:51:16 +08:00
|
|
|
if (gazeTimer >= gazeTimeRequired && !isDisplaying)
|
|
|
|
{
|
2025-02-08 22:11:47 +08:00
|
|
|
CollectInfo(targetObject);
|
2025-02-07 13:51:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2025-02-08 22:11:47 +08:00
|
|
|
// Reset timer when looking at a new object
|
|
|
|
currentObject = targetObject;
|
2025-02-07 13:51:16 +08:00
|
|
|
gazeTimer = 0f;
|
|
|
|
}
|
2025-02-07 11:15:46 +08:00
|
|
|
}
|
|
|
|
}
|
2025-02-07 13:51:16 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Reset if no valid object is in view
|
|
|
|
currentObject = null;
|
|
|
|
gazeTimer = 0f;
|
|
|
|
}
|
2025-02-07 11:15:46 +08:00
|
|
|
}
|
|
|
|
|
2025-02-07 14:37:22 +08:00
|
|
|
// Function to display object information
|
2025-02-07 11:15:46 +08:00
|
|
|
void CollectInfo(GameObject obj)
|
|
|
|
{
|
2025-02-07 14:37:22 +08:00
|
|
|
// Prevents spamming of display
|
2025-02-07 11:15:46 +08:00
|
|
|
isDisplaying = true;
|
2025-02-07 14:37:22 +08:00
|
|
|
|
2025-02-08 22:11:47 +08:00
|
|
|
// **Fix: Mark object as collected**
|
|
|
|
collectedObjects.Add(obj);
|
|
|
|
|
2025-02-07 14:37:22 +08:00
|
|
|
// Displays information
|
2025-02-08 22:11:47 +08:00
|
|
|
infoText.text = "<size=15>Info Collected:</size>\n\n" +
|
|
|
|
"<size=20>" + obj.name + "</size>\n" +
|
|
|
|
"<size=16>" + GetObjectInfo(obj) + "</size>";
|
2025-02-07 11:15:46 +08:00
|
|
|
Debug.Log("Collected information from: " + obj.name);
|
2025-02-08 22:11:47 +08:00
|
|
|
|
|
|
|
// Play sound only if no other sound is currently playing
|
|
|
|
if (!audioSource.isPlaying)
|
|
|
|
{
|
|
|
|
audioSource.PlayOneShot(scribbleSound);
|
|
|
|
}
|
2025-02-07 11:15:46 +08:00
|
|
|
|
2025-02-07 14:37:22 +08:00
|
|
|
// Clears text after displayed time
|
2025-02-07 13:51:16 +08:00
|
|
|
Invoke(nameof(ClearText), displayTime);
|
2025-02-07 11:15:46 +08:00
|
|
|
}
|
|
|
|
|
2025-02-07 14:37:22 +08:00
|
|
|
// Function to clear text after a delay
|
2025-02-07 11:15:46 +08:00
|
|
|
void ClearText()
|
|
|
|
{
|
2025-02-07 14:37:22 +08:00
|
|
|
// Removes text
|
2025-02-07 11:15:46 +08:00
|
|
|
infoText.text = "";
|
2025-02-07 14:37:22 +08:00
|
|
|
|
|
|
|
// Allows new information to be displayed
|
2025-02-07 11:15:46 +08:00
|
|
|
isDisplaying = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
string GetObjectInfo(GameObject obj)
|
|
|
|
{
|
2025-02-07 14:37:22 +08:00
|
|
|
// Check if the object's name is the same
|
2025-02-08 22:11:47 +08:00
|
|
|
if (obj.name == "Needles")
|
|
|
|
// Returns predefined information
|
|
|
|
return "A used needle. If my parents finds out they would murder me.";
|
|
|
|
|
|
|
|
if (obj.name == "Bottles")
|
2025-02-07 14:37:22 +08:00
|
|
|
// Returns predefined information
|
2025-02-08 22:11:47 +08:00
|
|
|
return "An empty bottle. It numbs the pain... but not for long enough";
|
2025-02-07 14:37:22 +08:00
|
|
|
|
|
|
|
// Default information if there is no specific case
|
2025-02-07 11:15:46 +08:00
|
|
|
return "An unknown object with mysterious origins.";
|
|
|
|
}
|
2025-02-07 13:51:16 +08:00
|
|
|
}
|