- Home /
Restricting a key press to when youre in a trigger?
So im having a bit of a problem. Say when i enter a trigger, text pops up to say "hey welcome to the shop, press "blank" to open it and press "blank" to close." i want to be able to open the shop with two different keys to open and close it ONLY when im in the trigger. right now my script causes me to open it all the time.
var stringToEdit : String = "---"; static var isHovering=false; static var shopOpen=false;
function OnTriggerEnter(){ isHovering = true; } function OnTriggerExit(){ isHovering = false; shopOpen = false; }
function OnGUI(){ if(isHovering){ stringToEdit = GUI.TextArea (Rect (200, 250, 400, 100), stringToEdit, 115); } if(shopOpen){ GUI.Box(Rect(0,0,Screen.width,Screen.height),"SHOP"); } }
function Update(){ if(Input.GetButton("Interact")){ shopOpen = true; isHovering = false; } if(Input.GetButton("Close")){ shopOpen = false; isHovering = true; } }
Btw, um... im kinda new here and i dont know how to put the script into that nice block of text.
Answer by Scribe · May 17, 2011 at 09:10 AM
var WelcomeMessage = "hey welcome to the shop, press 'B' to take a look at my wares"; var CloseMessage = "Press 'B' again to close the shop"; var isInShop = false; var shopOpen = false; var ShowWelcome = false; //var isOpen = false; var MyWidth = 400; var MyHeight = 100;
function OnTriggerEnter(){ isInShop = true; ShowWelcome = true; }
function OnTriggerExit(){ isInShop = false; ShowWelcome = false; shopOpen = false; }
function Update(){ if(isInShop){ if(!shopOpen){ if(Input.GetKeyUp(KeyCode.B)){ ShowWelcome = false; shopOpen = true; } } else { if(Input.GetKeyUp(KeyCode.B)){ shopOpen = false; } } } }
function OnGUI(){ if(isInShop){ if(ShowWelcome){ GUI.Box (Rect ((Screen.width/2)-(MyWidth/2), (Screen.height/2)-(MyHeight/2), MyWidth, MyHeight), WelcomeMessage); } }
if(shopOpen){
GUI.Box(Rect(0,0,Screen.width,Screen.height),"SHOP");
GUI.Label(Rect((Screen.width/2)-100, Screen.height-30, 200, 30), CloseMessage);
}
}
you could try that ^
When you enter the trigger it will show the welcome message and when you leave the area it closes the welcome message
It opens the shop interface when you press B and closes with the same button press but that can be easily changed
It shows the word 'SHOP' at the top of the interface and at the bottom it tells you how to close the shop
When the shop closes it does not show the welcome message (I thought that would be better as when your leaving you don't want to be saying hello) you could have a different message showing instead if you wanted like 'thank you for shopping' etc
if you leave and then re enter the shop it will show the welcome message again
Hope thats helpful to you
Scribe
Answer by FreeTimeDev · May 17, 2011 at 08:05 AM
It's opening all the time, as soon as you enter it, because you've made it so that as soon as they enter the trigger (without any input) the boolean for the shop is true. Thus, triggering the GUI to display the shop window.
Put isHovering inside an if statement for a GetButtonDown to toggle the boolean. another button to close it, or re use the first one to toggle.
on your trigger exit, you probably want it to ALWAYS close, regardless, so leave it.