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 Kikoualoe · May 17, 2011 at 06:44 AM · guicollidertriggerkey

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.

Comment
Add comment · Show 1
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 FreeTimeDev · May 17, 2011 at 06:47 AM 1
Share

Highlight it all, and click the "1010101" icon.

2 Replies

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

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

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 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.

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

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

Handling mouse events in objects inside a trigger? 0 Answers

Is there a script that will trigger a gui texture when an object is destroyed? 2 Answers

Press button if you are inside a trigger (Sorry) 2 Answers

Trigger GUI working wrong 1 Answer

Can't click gameobject when over another trigger? 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