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
|
|
|
|
public float gazeTimeRequired = 2f;
|
|
|
|
|
|
|
|
// Defines how long the information UI will remain on screen
|
|
|
|
public float displayTime = 5f;
|
|
|
|
|
|
|
|
// 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-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-07 14:37:22 +08:00
|
|
|
// If the player is still looking at the same object, the gaze time
|
2025-02-07 13:51:16 +08:00
|
|
|
if (currentObject == hit.collider.gameObject)
|
|
|
|
{
|
2025-02-07 14:37:22 +08:00
|
|
|
// Increases gaze time
|
2025-02-07 13:51:16 +08:00
|
|
|
gazeTimer += Time.deltaTime;
|
2025-02-07 14:37:22 +08:00
|
|
|
|
|
|
|
// If gaze time reaches required time and is not displaying yet, display the information
|
2025-02-07 13:51:16 +08:00
|
|
|
if (gazeTimer >= gazeTimeRequired && !isDisplaying)
|
|
|
|
{
|
2025-02-07 14:37:22 +08:00
|
|
|
// Display object information
|
2025-02-07 13:51:16 +08:00
|
|
|
CollectInfo(currentObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Reset timer if looking at a new object
|
|
|
|
currentObject = hit.collider.gameObject;
|
|
|
|
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
|
|
|
|
|
|
|
// Displays information
|
2025-02-07 11:15:46 +08:00
|
|
|
infoText.text = "Info Collected: " + obj.name + "\n" + GetObjectInfo(obj);
|
|
|
|
Debug.Log("Collected information from: " + obj.name);
|
|
|
|
|
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
|
|
|
|
if (obj.CompareTag("Needles"))
|
|
|
|
// Returns predefined information
|
2025-02-07 13:51:16 +08:00
|
|
|
return "Test";
|
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
|
|
|
}
|