• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By ethancodes
      I'm having a weird issue with detecting a collision. I've tried everything I could find online but nothing seems to work. I have a brick object. It has a 2D Collider attached and I have also attached a 2D Rigidbody on it. I also have an EndScreen 2D Collider. The EndScreen 2D collider is tagged with "EndScreen". I am trying to detect when a brick collides with the end screen collider and simply print "game over" in the console. 
      This is my current code for this part of the program, it is attached to the bricks:
      void OnCollisionEnter (Collision2D collision) { if (collision.gameObject.tag == "EndScreen") { Debug.Log("Game over"); } } Several things have happened depending on the set up. If I have the rigidbody 2D set as static, my ball object can still collide with the bricks, but I get no Log message. If I set it to Kinematic or Dynamic, I get absolutely no interaction between the ball and the bricks, and nothing when the bricks pass through the collider. I have tried to set the collider to a trigger and use OnTriggerEnter2D, no change. I have tried to put the rigidbody on the EndScreen object and tried to set it's body type to all 3 settings, no change. The only thing I can think of that I have not done is put the script on the EndScreen object and switch the tag to the bricks. The reason I have not done this is because I will have several types of bricks, some of which will have different tags. 
       
      Please tell me somebody can see what I'm doing wrong here, because I'm losing my mind over something I feel should be ridiculously simple. Thanks.
    • By Sandman Academy
      Downloadable at:
      https://virva.itch.io/sandman-academy
      https://gamejolt.com/games/sandmanacademy/329088
      https://www.indiexpo.net/en/games/sandman-academy
      https://www.gamefront.com/@sandmanacademy
      http://www.indiedb.com/games/sandman-academy
    • By Sandman Academy
      Downloadable at:
      https://virva.itch.io/sandman-academy
      https://gamejolt.com/games/sandmanacademy/329088
      https://www.indiexpo.net/en/games/sandman-academy
      https://www.gamefront.com/@sandmanacademy
      http://www.indiedb.com/games/sandman-academy
    • By Sandman Academy
      Downloadable at:
      https://virva.itch.io/sandman-academy
      https://gamejolt.com/games/sandmanacademy/329088
      https://www.indiexpo.net/en/games/sandman-academy
      https://www.gamefront.com/@sandmanacademy
      http://www.indiedb.com/games/sandman-academy
    • By Sandman Academy
      Downloadable at:
      https://virva.itch.io/sandman-academy
      https://gamejolt.com/games/sandmanacademy/329088
      https://www.indiexpo.net/en/games/sandman-academy
      https://www.gamefront.com/@sandmanacademy
      http://www.indiedb.com/games/sandman-academy
  • Advertisement
  • Advertisement
Sign in to follow this  

Unity Programming commands

This topic is 1004 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