Search Unity

Press vs Hold (Jumping)

Discussion in 'Scripting' started by FIZZZGAR, Apr 16, 2019.

  1. FIZZZGAR

    FIZZZGAR

    Joined:
    Apr 15, 2019
    Posts:
    2
    Hello, I'm still fairly new to unity and following tutorials in order to learn/improve. In one of the tutorials, a script was made in order to change jump height (higher or lower) depending on if the user pressed or held down the button. It works if I use the space bar, but when I use the touch screen on my computer or click with the mouse it does the same jump height each time. How would one make this work for touch screen ex. for a mobile game where you might not have access to buttons.

    Edit: I thought it would be easier if you can see my script so I added it. Also i was thinking that if i can't figure out the issue with the touchscreen then i would create a button that basically acted like a space bar for when the game is played on a mobile device.
     

    Attached Files:

    Last edited: Apr 16, 2019
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    How did it work for keyboard buttons, and how did you try to adapt it to touchscreen/mouse?

    Probably this ultimately boils down to whether you're checking if a button is currently down or checking if a button was just pressed.

    For keyboard input,
    Input.GetKeyDown() returns true if the button was just pressed
    Input.GetKey() returns true as long as the key is currently down

    Touches have a TouchPhase that describes what the touch is doing; you can use that to distinguish between a new touch and an ongoing touch.

    There's a few different ways to get mouse input in Unity. If you are using the IPointerDownHandler interface, you may need to create your own variables to keep track of whether the pointer is currently being held down (using IPointerUpHandler to detect when the mouse is released).
     
    Kurt-Dekker likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    If you're using they keyboard, Input.GetKey() and Input.GetKeyDown() tell you if a key IS down, or if a key WENT down.

    There is equivalents for the mouse, as well as touchscreen.

    You can use Input.GetMouseButton() and Input.GetMouseButtonDown() for the same things: IS down vs WENT down.

    But if you want more touches (like multiple finger) then you need to process Input.Touches.

    I have an open source ProximityButtons project that has a MicroTouch structure in it that lets you use Touches always, even when on the PC in the Editor.

    proximity_buttons is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/proximity_buttons

    https://github.com/kurtdekker/proximity_buttons

    https://gitlab.com/kurtdekker/proximity_buttons