Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Keypress Prefab

Discussion in 'Editor & General Support' started by Fersutagames_, Oct 6, 2015.

  1. Fersutagames_

    Fersutagames_

    Joined:
    May 2, 2013
    Posts:
    52
    Hi!

    I've been doing some research and have been trying to come up with a solution but so far with no luck :(

    what i'm hoping to achieve is that when you press the Cube prefab i have on the screen with the mouse it will then simulate me pressing the Space key.

    So far this is what i've got with no progress..

    Code (JavaScript):
    1. function OnMouseDown () {
    2.  
    3. if (Input.GetMouseButtonDown(KeyCode.Space))
    4.            
    5. }
    Any ideas?
     
  2. McMayhem

    McMayhem

    Joined:
    Aug 24, 2011
    Posts:
    443
    Well first of all, you cant use Input.Get in the way you're trying to use it. Input.GetKeyDown/MouseButtonDown is a function that returns true if the given keycode is currently down. It doesn't simulate a keypress using the passed keycode. Also, Input.GetMouseButtonDown(KeyCode.Space) would never return true as that particular function looks for mouse input only (0, 1, 2, etc).

    This UnityAnswer is a pretty good example of how to simulate keypresses via code. However, if it isn't absolutely necessary for you to simulate the "key" but rather the function attached to that key, then it's as easy as making that function accessible to this portion of the script and calling it using this method:

    Code (CSharp):
    1. if(Input.GetMouseButtonDown(0)) //0 = Left Mouse 1 = Right Mouse
    2. {
    3. //This would be where you put the function call that occurs
    4. //normally when the player presses SPACE
    5.     SpaceBarFunction();
    6.  
    7. }
     
  3. holliebuckets

    holliebuckets

    Moderator

    Joined:
    Oct 23, 2014
    Posts:
    496
    So I am extremely interested in this :) Would you be able to attach a pic of the Profiler, especially during the loading hiccup?? :D
     
    Fersutagames_ likes this.
  4. Fersutagames_

    Fersutagames_

    Joined:
    May 2, 2013
    Posts:
    52
    Thanks for the answer McMayhem! i dont seem to be having much luck with Javascripts.. i think i have a basic outline of where im going.. if someone could correct my script, or help me find something similar it'd be much appreciated!

    Code (JavaScript):
    1. function OnTriggerEnter(Col : Collider){  //detect mouse click?
    2.  
    3. if(Input.GetMouseButtonDown(0)) //detects that user has clicked on the prefab
    4.    
    5.      Input.GetKey("Space") //when prefab is clicked, simulate that the space bar was pressed
    6. }
    i think i may be getting closer! Hopefully! :p
     
  5. Darhkuan

    Darhkuan

    Joined:
    Feb 24, 2014
    Posts:
    21
    Fersut - is there any reason why you need to simulate the key press?
    As per mayhems comment, the keypress is only a stepping stone to doing something usually (i.e. normally when I press space bar, its because I want a space). You could use his example of a function to just skip the input part, as really what your doing is effectively creating a virtual keyboard, why bother with the inputs side of the world?

    Code (CSharp):
    1. public string OnSpaceBarClick()
    2.  
    3. {
    4. return " ";
    5. }
    You could use the above as a way of getting that value back anyhow.

    Also Input.GetKey returns a Boolean value based on whether the key has been pressed or not, so wont work in your script above.
     
    McMayhem likes this.
  6. holliebuckets

    holliebuckets

    Moderator

    Joined:
    Oct 23, 2014
    Posts:
    496
    haha i totally responded to the wrong thread (too many tabs open) and now I can't find the post I was looking at yesterday >.<
     
  7. McMayhem

    McMayhem

    Joined:
    Aug 24, 2011
    Posts:
    443
    I think we're all having mental hiccups today :D. I just realized I talked about a UnityAnswer post without ever actually linking it in the post! Here is the post I was referring to that talks about how to simulate keypresses.

    You'll not that from that post, it's a pretty complicated process as there is no native way to simulate a keypress in raw form with Unity's vanilla API. The best way to do it is to have a single standard function that handles what to do when pressing space, then access that function when clicking on the object.

    Example Spacebar Code:
    Code (CSharp):
    1. public void OnSpaceDown()
    2. {
    3.      Debug.Log("Space bar was pressed!");
    4. }
    You would then call OnSpaceDown() whenever you wanted to simulate that keypress. This will not work if your desire is to have some external program register that the spacebar was pressed. This will only work within Unity. (That's the only scenario I can think of where you'd actually need to simulate raw input)

    As for your way of handling the code, I think I have the general idea of what you're trying to accomplish. It seems like you want to simulate the space key when the player clicks on the object. If that's the case then we need to make some adjustments to your code.

    Let's break it down:
    Code (JavaScript):
    1. //Actually, OnTriggerEnter only detects collision with other objects in the scene, and has nothing to do with mouse clicks
    2. function OnTriggerEnter(Col : Collider){  //detect mouse click?
    3. if(Input.GetMouseButtonDown(0)) //detects that user has clicked on the prefab
    4.    //Input.GetKey, Input.GetKeyDown, Input.GetKeyUp all return a boolean (true or false) and thus would not do anything right here. This is where you would execute your OnSpaceDown() function
    5.      Input.GetKey("Space") //when prefab is clicked, simulate that the space bar was pressed
    6. }
    Since you want this to happen when the player clicks on the object, you have to use a different event called "OnMouseDown" which does actually require a trigger collider to be attached to the object.

    This is what it should look like:
    Code (CSharp):
    1.  void OnMouseDown() {
    2.         OnSpaceDown(); //Simulate the spacebar
    3.     }
    Hope that helps a bit.
     
    Darhkuan likes this.