Script that shows a text to press "E" on the object?

how can i put a script that displays the text “Press E” once the character looks at the object to pick it up? eg. My character wants to pickup a key button doesn’t know when he can press it so he moves around until the popup see “Press E”.

Anyone have a script for this that I can use?

Here is a quick script I just created that shows the prompt to press the E key if you are on the key:

Create a cube, resize it so it’s big enough and set it as a trigger and attach this script to it:

var hasCollided : boolean = false;
var labelText : String = "";

function OnGUI()
	{
		if (hasCollided ==true)
	{	
 		GUI.Box(Rect(140,Screen.height-50,Screen.width-300,120),(labelText));
	}
}

function OnTriggerEnter(c:Collider)
	{
		if(c.gameObject.tag =="Player") 
	
	{

		hasCollided = true;
		labelText = "Hit E to pick up the key!";
		
}
}

function OnTriggerExit( other : Collider ){
 	hasCollided = false;
 
}

If you need any help figuring out how to pick the key up, let me know and I will help you out.

This script detects if the player has collided with the trigger. If they have, set the hasCollided boolean to true and print the prompt. If they exit the trigger, get rid of the GUI box and so on. You can also make it so if the key has been picked up, destroy this gameObject so the prompt does not appear once the key has been picked up. Let me know if you need anything.

I’m currently doing this in a 3rd person perspective and I’m achieving this in a RayCast solution. Some pseudo-code that might help you:

  1. Tag your “interactable item” as “Interactable” (or whatever you want to call the tag
  2. Have a script that is in charge of interacting and in the Update() method check if ‘e’ is down
  3. If ‘e’ is down shoot a ray from your player.transform.position in the player.transform.forward direction for a small length (I use 0.5f)
  4. Check the hit of the raycast like ‘hit.collider.gameObject.tag == “Interactable”’ and do whatever you feel appropriate (in your case toggle a gui visibility variable)

Good luck, it’s totally do-able!

Here’s a clue: Unity - Scripting API: Camera.WorldToViewportPoint

Create a GUI Text in the scene saying press E and then disable it.

Then create a JS with the code below and add it to all item you want to use it.

Set Gui to the one you just created and disabled, set The player as you character, set The detection radius,

Good Luck


var distance;

var Gui : GameObject;

var Player : Transform;

var DetectDistance = 15.0;

function Update () 
{


distance = Vector3.Distance(Player.position, transform.position);

if(distance < DetectDistance)
{
Show ();

}
if(distance > DetectDistance)
{
Hide (); } }

function Show ()
{

Gui.active = true;
}

function Hide ()
{

Gui.active = false;
}

In case anyone else finds this thread, here’s some code that I wrote based on some of the suggested comments. It sits on the main camera and detects nearby objects and allows you to interact with them. The interact key should be specified in the project settings, allowing it to be changed to preference.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class rayCastInteractPrompt : MonoBehaviour
{
    [SerializeField]
    [Tooltip("This message will be shown when we are near an interactable item.")]
    private string labelText;//message to show
    private bool interactableDetected;
    private RaycastHit vision;//used for detecting raycast collision
    [SerializeField]
    private float rayLength;//used for assigning length to raycast
    private bool buttonInUse = false;
    [SerializeField]
    [Tooltip("Percentage of screen width that prompt takes up.")]
    private float promptWidthPercent;
    private float promptWidthCalculated;
    private float promptXValue;

    [SerializeField]
    [Tooltip("Percentage of screen height that prompt takes up.")]
    private float promptHeightPercent;
    private float promptHeightCalculated;

    private void Start()
    {
        promptWidthCalculated = Screen.width * (promptWidthPercent * .01f);//divide by 100 to convert to percent. Multiplication works the same but is faster.
        promptXValue = (Screen.width * 0.5f) - (promptWidthCalculated * 0.5f);

        promptHeightCalculated = Screen.height * (promptHeightPercent * .01f);//divide by 100 to convert to percent. Multiplication works the same but is faster.
    }

    private void Update()
    {
        if (Time.frameCount % 3 == 0)//runs these functions every three frames to save cpu cycles
        {
            Debug.DrawRay(transform.position, (transform.forward * rayLength), Color.red, 0.5f);

            if (Physics.Raycast(transform.position, transform.forward, out vision, rayLength))
            {
                if (vision.collider.tag == "npc")
                {
                    //Debug.Log("NPC spotted!");
                    interactableDetected = true;
                }
                else
                {
                    interactableDetected = false;
                }
            }
            else
            {
                interactableDetected = false;
            }

            if(interactableDetected)
            {                

                if(Input.GetAxisRaw("Interact") != 0)
                {
                    if(!buttonInUse)
                    {
                        //Debug.Log("button pressed");
                        vision.collider.gameObject.GetComponent<interact>().interaction();//run function here
                        buttonInUse = true;
                    }
                }
                else
                {
                    //Debug.Log("button not pressed");
                    buttonInUse = false;
                }
            }
        }
    }    


    void OnGUI()
    {
        if (interactableDetected == true)
        {
            GUI.Box(new Rect(promptXValue, 0, promptWidthCalculated, promptHeightCalculated), (labelText));
        }
    }    
}

We made a tutorial similar to this method. You can check it out here. It works perfectly and can be adjusted however you like.