Displaying an item's description on screen using the GUI (Resident Evil style)
Hello! This is a follow up to my previous question about setting up the interactions (http://answers.unity3d.com/questions/1245359/cant-get-simple-interaction-code-resident-evil-sty.html#comment-1245390), which I solved using raycasts to detect the object and output its description on the console for testing purposes. Now, I want to integrate the GUI system to display a text on the lower center part of the screen. In case you don't have time to read the previous question, here's what I want to achieve: walk up to the object, press the interaction key, pause the game, display the item's description as a GUI text and press Interact again to return the flow of the game and erase the description. The problem is that I don't know how to do that since I have to use OnGUI (you'll see what I'm talking about below, I'll provide the relevant code with explanation for simplicity's sake)
There are three scripts that are relevant to this task: PlayerController (controls the player and checks for interactions), InteractiveProp (added on the items, only contains the public string itemDescription so far) and GameManager which, for the time being, I use to store the definitions of the functions for convenience. In my PlayerController script, I have the following code:
void Update()
{
// Detect various interactions
if (Input.GetButtonDown ("Interact"))
{
gmHandle.InteractWithProp();
}
}
where gmHandle is an object of type GameManager that I use to access the script and subsequently, it's InteractWithProp() function. Next is my GameManager script:
using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour {
// ---------- THIS SCRIPT IS USED TO MANAGE THE GAME AND STORE FUNCTIONS TEMPORARILY OR PERMANENTLY --------- //
public PlayerController playerHandle;
public GameObject playerGameObject;
public float textX,textY;
void Start ()
{
playerGameObject = GameObject.FindGameObjectWithTag("Player");
playerHandle = playerGameObject.GetComponent<PlayerController>();
}
void Update ()
{
}
void OnGUI()
{
if (playerHandle.isInteracting)
{
Rect test = new Rect(textX,textY,Screen.width/4,Screen.height/4);
GUI.Box (test,"This is a test text!");
}
}
// --------DEVELOPER DEFINED FUNCTIONS-------- //
// Tests if there's an interactive object and activates its properties
public bool InteractWithProp()
{
if (!playerHandle.isInteracting)
{
Debug.Log ("Player Pressed E");
// Cast a ray from the player forward to detect object (DO: IMPLEMENT LAYER MASK FOR OPTIMIZATION)
RaycastHit hitInfo;
Vector3 rayCorrection = new Vector3(0f,0.5f,0f); // Raises the casted ray above ground
if (Physics.Raycast ((playerGameObject.transform.position) + rayCorrection ,playerGameObject.transform.forward,out hitInfo,2f))
{
if (hitInfo.collider.tag == "InteractiveObject")
{
Debug.Log ("Succesful Interaction!");
// Pause the game
Time.timeScale = 0;
// Get the item description
string propDesc = hitInfo.collider.gameObject.GetComponent<InteractiveProp>().itemDescription;
Debug.Log (propDesc);
}
playerHandle.isInteracting = true;
}
}
else
{
Time.timeScale = 1f;
Debug.ClearDeveloperConsole();
playerHandle.isInteracting = false;
}
}
}
What you see in here regarding the GUI is only for test purposes so far. As I said, what I want to do is access the itemDescription string of the object that was detected and output that as text on the screen. But I'm not quite familiar with OnGUI() and I don't know how to access that variable, since it is only accessed inside InteractWithProp() if and only if there's a ray hit. I could possibly include another call to the function in OnGUI() but I don't think it's efficient.
Do you have any ideas on how I could solve that? Any other suggestions will be much appreciated!
Your answer
Follow this Question
Related Questions
How to make pie menu options interact to OnMouseDown? 0 Answers
webgl app is not working in browser 0 Answers
How do you get different parts of the same object to move in different ways? 1 Answer
My collisions won't work whenever I pick it up with the XR Toolkit 0 Answers
Unity 3D #C Nightvision does not want to switch on or off. 0 Answers