Jump to content
  • Advertisement
Sign in to follow this  
dirmp

Unity Programming commands

This topic is 1093 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Good night, all right?
I am new to Unity, despite having already completed some basic projects, and I am thinking about the best way to organize the code I use to commands, for example, when you press the key "i", open inventory, among others...
The way I'm doing it: I put almost all codes in a script within the CharacterController, but not sure if I'm doing the right way... For example, were various functions within the Update ():
 
if(Input.GetKeyDown(KeyCode.I))
{
//Open inventory
}

if(Input.GetKeyDown(KeyCode.Escape))
{
//Close the application
}
 
And there are many commands as well, I've thought about putting this script into an empty gameObject so I can import into any scene. And one more question: So, each frame is scanned all if/else to find some valid to run, but does not use a lot of memory? Or is there another more correct way to use scripts for each keyboard command?
 
Thank you!!
Edited by dirmp

Share this post


Link to post
Share on other sites
Advertisement

It is fairly common to have a key mapper. This allows players to customize the keys in use, and also works with a state machine to ensure only the valid keys are processed.

 

So you can detect if you are in the main game, and then map a variable key (which by default you mapped to "i") to an event that triggers the inventory UI.  Elsewhere in your game, or in unity's built-in setup tool, allow the player to map the key to anything else, perhaps they like the "p" key or "\" key because it is similar to what they've got in another game. 

 

Similarly, ESC could be mapped to many different events based on the state of the game. On some screens it could close the dialog, inside some transactions dialogs it could cancel the transaction, inside the main game it could pop up the menu, inside some menus it could back out one level or prompt to quit the game if at the root level.

 

Edit: Here's a link to Unity's Input Mapper class, if you need it.  Again, note that instead of looking for a specific KeyCode value you would name a button, perhaps one called "Inventory" mapped to 'i' by default. You would test it with Input.GetButtonDown("Inventory").

 

Ok, I got it, thanks for the tip .. I will find out more about key mapper! But on my other questions, I keep using the same algorithm I'm using?

Share this post


Link to post
Share on other sites
Every update do a big switch statement that jumps between each of your game modes, in each game mode check for the button down status, and trigger the events as necessary.

If you're in the main menus, generate the menu events and broadcast them. If you're in the main game play, generate those events, if you're in the pause menu, generate those events. If you are in some other mode, maybe a dialog mode or interactive movie mode or qte event mode, generate and broadcast those appropriate events.

Share this post


Link to post
Share on other sites

Ok, thank you for the answer !! Was of great help ... Then share the final result!

Greetings!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!