- Home /
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; }
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.?
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?
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??
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.
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??
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...'
Your answer
Follow this Question
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