Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ReedMon · Nov 11, 2012 at 05:53 PM · buttontextdisplayshowse

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

6 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ReedMon · Nov 12, 2012 at 10:57 PM 0
Share

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"){

avatar image Nercoe · Nov 12, 2012 at 11:29 PM 0
Share

I updated the script to fit what you need. Change "Player" to the tag that should collide.

avatar image ReedMon · Nov 12, 2012 at 11:48 PM 0
Share

Thanks! I'll ask if I have any troubles

avatar image Nercoe · Nov 12, 2012 at 11:51 PM 0
Share

No problem mate, I hope I helped you understand :)

avatar image ReedMon · Nov 13, 2012 at 01:03 AM 0
Share

Haha you did! Thanks!

Show more comments
avatar image
1

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:

  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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by DaveA · Nov 12, 2012 at 08:59 PM

Here's a clue: http://docs.unity3d.com/Documentation/ScriptReference/Camera.WorldToViewportPoint.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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));
         }
     }    
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I set a GUI button's text using a string from another script? 0 Answers

Display a Text/GUI on screen when triggerd with Fadein/Fadeout 1 Answer

[closed]Move Buttons and Text 1 Answer

How to display EULA from Doc in Unity? 0 Answers

How to get new text from this? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges