Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Assigning Keys

Discussion in 'Editor & General Support' started by AaronC, Sep 25, 2006.

  1. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Cant figure this out. Read all the docs and dont get it. Im trying to assign an input key-"r" to instsntiate something.
    Using:
    Code (csharp):
    1. var newObject : Transform;
    2. function Update () {
    3.  
    4.  if (Input.GetButtonDown("r")) {
    5.  
    6.     Instantiate(newObject, transform.position, transform.rotation);
    7.  
    8.  }
    9. }
    10.  
    To instantiate.
    Have done this(see input panel pic)

    To set up input, but its obviously not right. I have also found this:
    Code (csharp):
    1.  
    2. value = Input.GetKey ("r");
    do I add the second code snippet to the first? Doesnt seem to work...
    Thanks for some insight
    AC
     

    Attached Files:

  2. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Sorry to double post but I just figured out my 2 mistakes..
    Hope this is helpful out there...

    Feel free Unity crew to delete this post.

    Correct script:
    Code (csharp):
    1. var newObject : Transform;
    2. function Update () { value = Input.GetKey ("r");
    3.  
    4.  if (Input.GetKey("r")) {
    5.  
    6.     Instantiate(newObject, transform.position, transform.rotation);
    7.  
    8.  }
    9. }
    10.  
    note getkey, not get button down
    and the correct input setup:

    Sorry 4 wasting yr time
    AC
     

    Attached Files:

  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The problem with that is that you've now hardcoded "r" as the instantiate button. Also, using Input.GetKey is going to instantiate an object every frame the key is held down, isn't it?

    Use this instead:

    Code (csharp):
    1.  
    2. var newObject : Transform;
    3. function Update () {
    4.  
    5.  if (Input.GetButtonDown("Instantiate")) {
    6.  
    7.     Instantiate(newObject, transform.position, transform.rotation);
    8.  
    9.  }
    10. }
    11.  
    You were right the first time, but you were referring to GetButtonDown by the key (which can be anything), rather than by the name (although you figured that out). ...Of course, for the above code to work, you would change the name from "r" back to "Instantiate"...which you should, because "Instantiate" is way more descriptive than "r". ;) Though perhaps "InstantiateObject" would be best, just to distinguish it more from the Instantiate command. Anything to keep your scripts more descriptive and clear helps, so when you go back and look at it a few weeks/months later, you don't go "huh?"

    --Eric
     
  4. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Thanks Eric-Thats v helpful
    AC