- Home /
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?
Answer by Nercoe · Nov 12, 2012 at 09:25 PM
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.
How would I change it so that it only displays on the tag? im not sure how to add it, I believe it's like if (gameObject.tag ="$$anonymous$$ey" hasCollided ==true)
Not sure really. Still learning.
EDIT: I remember writing this myself on one of my own scripts if(col.gameObject.tag == "Player"){
I updated the script to fit what you need. Change "Player" to the tag that should collide.
Answer by Yokimato · Nov 12, 2012 at 09:45 PM
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:
Tag your "interactable item" as "Interactable" (or whatever you want to call the tag
Have a script that is in charge of interacting and in the Update() method check if 'e' is down
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)
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!
Answer by DaveA · Nov 12, 2012 at 08:59 PM
Here's a clue: http://docs.unity3d.com/Documentation/ScriptReference/Camera.WorldToViewportPoint.html
Answer by Tlc indie · Nov 13, 2012 at 01:03 AM
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;
}
Answer by Michael_Berna · Nov 18, 2019 at 09:49 PM
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));
}
}
}