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 Michael 12 · May 04, 2011 at 07:23 PM · guipopup

Make a pop up window work on a key press

Is there not a way to get this to work so as when the player is close to the game objects, and ONLY facing the object that by clicking on it with either the mouse or by pressing a key [Q] for example, that the window will pop up? And press [Q] Again to close it.

I need something like an instructional series of pop ups for game objects inside of a house which is where my FPS player will start the game. So instruction and info about objects would be good. As it is right now in it's current incarnation, the window will open no matter where you are in the game!

I've been trying to mod this script which for the most part is ideal, but needs to be tweaked so you don't fire your gun every time you want it to open and ONLY open when you are right in front of an object that has information about it.

Also would be great if this could be somehow made to access multiple info windows for many different game objects... I'm surprised there is not something like this already pre made and set up as a popular game mechanic??!!

(See Below) that I found on this most excellent question http://answers.unity3d.com/questions/8144/trying-to-pick-up-and-see-paper-pop-up-gui-window-to-examine-objects-when-click

It's perfect for what I need ONLY thing is, is that when I use my mouse pointer and then press fire on my mouse I waste ammo just oppening the pop up window.

So how can I make this where the pop up will open when my player presses [Q] key and have it close by using the [Q] key also.

also what would make this even more perfect is that if you walk away from the key the window closes automatically.

How can I do this?

EDIT

THIS IS THE SCRIPT THAT GOES ON THE GAME OBJECT TO BE CLICKED

var popupTexture : Texture2D; private var keyPopup : KeyPopup;

function Start () { keyPopup = FindObjectOfType(KeyPopup);

     if (popupTexture == null) {
         popupTexture = renderer.material.mainTexture;



     }

}

function OnMouseOver (){

     keyPopup.Show(this);


}

EDIT

THIS IS THE SCRIPT THAT GOES ON THE GUI TEXTURE (Popup) I updated this with What Dave gave me and it works on a key press But how to make this so it works ONLY when my FPS player is facing the object and ONLY when close enough to pick up the object?? :

function Start() { Hide(); }

function Hide() { guiTexture.enabled = false; }

function Show(key : Key) {

     if (!guiTexture.enabled) {
         var t = key.popupTexture;
         var rect = Rect( -t.width/2, -t.height/2, t.width, t.height );
         guiTexture.pixelInset = rect;
         guiTexture.texture = t;
         guiTexture.enabled = true;


     }

}

function Update() { // see if we are looking in the general direction of this thing, and within 2 meters of it var viewPos : Vector3 = Camera.main.WorldToViewportPoint (transform.position); if ((viewPos.x > .1 && viewPos.x < .9 && viewPos.z > 0) && (transform.position - Camera.main.transform.position).magnitude < 2.0) { if (Input.GetKeyDown("Action")) guiTexture.enabled = !guiTexture.enabled; } else // looking away, or too far away guiTexture.enabled = false; }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DaveA · May 04, 2011 at 09:15 PM

Add:

    function Update()
    {
      // see if we are looking in the general direction of this thing, and within 2 meters of it
      var viewPos : Vector3 = Camera.main.WorldToViewportPoint (transform.position);
      if ((viewPos.x > .1 && viewPos.x < .9 && viewPos.z > 0) && (transform.position - Camera.main.transform.position).magnitude < 2.0)
      {
        if (Input.GetKeyDown("q"))
        {
          if (!guiTexture.enabled)
           Show();
          else
            Hide();
//          guiTexture.enabled = !guiTexture.enabled;
      }
      else // looking away, or too far away
        guiTexture.enabled = false;
    }

That will let the q key toggle it on/off. Assumes this script is on the 'key' or whatever object-of-interest.

As for walking away, do you want to do that while backing away (still can see the object), or turning away, or some minimum distance involved, etc.?

Comment
Add comment · Show 15 · 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 Michael 12 · May 04, 2011 at 09:38 PM 0
Share

Thanks Dave that solved part of the problem. ;) I'm trying to get it so it only pops up if you are facing it and olny if you are close enough to pick up the object?

avatar image Michael 12 · May 04, 2011 at 09:56 PM 0
Share

The only other thing I just discovered is that now no matter where I am if I press the [Q] key that GUI popup window will pop up??

avatar image DaveA · May 04, 2011 at 11:45 PM 0
Share

Added code to check that this thing is in view, and then q will work. If not near and looking at the thing, no GUI visible.

avatar image Michael 12 · May 05, 2011 at 02:06 AM 0
Share

Hmmm Not working at all, should I be putting this Update in the other script as well as this one? And should that: function On$$anonymous$$ouseExit() { Hide(); Part be removed at all??

avatar image DaveA · May 06, 2011 at 04:23 PM 0
Share

Remove the On$$anonymous$$ouseExit. It should work, because the first 'if' says 'if this-thing-the-script-is-on is in view and less than 2 meters away...'

Show more comments

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

No one has followed this question yet.

Related Questions

GUILayout.Popup bug? 0 Answers

Activate Gui Texture On Trigger Activate 1 Answer

problem with a pop-up window 1 Answer

OnTriggerEnter GUI 1 Answer

Not able to interact with GUI pop up menu 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