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. Dismiss Notice

Resolved OnScreenButton doesnt identify point exit?

Discussion in '2D' started by xDayhawk, Apr 21, 2023.

  1. xDayhawk

    xDayhawk

    Joined:
    Jun 24, 2020
    Posts:
    55
    Hi,

    I've started using the new input system for a pretty simple 2D Platformer, i got the movement working great with both the keyboard and gamepad.

    I've set it up for Mobile as well and created 4 buttons, Left Right Jump and Dash.
    Jump and Dash work fine, the left and right also work well but they don't seem to identify a pointer exist.

    What I mean is - if you press the Left button it moves left, if you press the Right button it moves right, but if you press the Left button and keep your finger on the screen and just slide it to the right button or basically anywhere you want on the screen, the left button remains clicked, the player keeps moving. (you can see that the button has a clicked animation still).
    You can also simulate it on playMode with your mouse of course.

    Here's a video where i try to explain the issue:


    Don't know if any of these details will help but:
    This is the input system set up
    upload_2023-4-21_22-8-8.png

    This is the button that mimics one of the movements, with the OnScreenButton script:
    upload_2023-4-21_22-9-48.png

    This is the movement code (I'm pretty sure it's useless for this problem, I feel like its more of something I'm missing with the OnScreenButton):
    New input system event handling
    Code (CSharp):
    1.     private void Movement_Performed(InputAction.CallbackContext context)
    2.     {
    3.         Vector2 inputVector = context.ReadValue<Vector2>();
    4.         _horizontal = inputVector.x;
    5.         desiredQuantity = _horizontal;
    6.  
    7.     }
    Movement code
    Code (CSharp):
    1.         if (isOnPlatform)
    2.         {
    3.                 _rb.velocity = new Vector2(currentQuantity * _movementSpeed + platformRB.velocity.x, _rb.velocity.y);
    4.         }
    5.         else
    6.         {
    7.                 _rb.velocity = new Vector2(currentQuantity * _movementSpeed, _rb.velocity.y);
    8.         }

    Help appreciated!
    This issue is driving me bananas!

    Thanks,

    P.S. - Yes i know i can probably do it in another way without using the New Input System, but i'm trying to keep the system as generic as possible.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    This is commonly called "finger latching" and is the most-useful way of input.

    This is how almost all UI buttons work in general, finger or mouse.

    Otherwise, you could click down on Button A, slide the mouse / finger to Button B and release... and Button B would fire!

    This is rarely correct. It can be, but it's just not... common.

    For a game I made with controls very much like what you have (left, right, jump, fire), I have made something like this and I call it a "Proximity Button Set." Some qualities:

    - touching anywhere within its area will activate one button only
    - it will always activate the closest button
    - it will remain activated until ANOTHER button becomes closer
    - upon finger release all buttons are released

    You're welcome to tinker with in the public project below. The repo even contains the precise left / right (one set) and jump / fire (another set) button demo. See the
    DemoProximityButtonsOnGUI
    or
    DemoProximityButtonsForUI
    scenes.

    I imagine you might be able to coerce the existing screen buttons you have to do it as well, I am just not familiar enough with them.

    It may be helpful to you to draw out a flowchart if you end up implementing it with EventTriggers, for instance.

    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

    https://sourceforge.net/projects/proximity-buttons/
     
  3. xDayhawk

    xDayhawk

    Joined:
    Jun 24, 2020
    Posts:
    55
    Thanks Kurt!

    I didnt know its called Finger latching, it actually makes a lot of sense and i've decided on leaving it like that for now, and introducing a joystick which is another choice of movement you can get via the Options page.

    And thanks for always helping out, you're always the first (and sometimes only) person who responds :) i checked your games and youtube channel, very cool!! big thumbs up!
     
    Kurt-Dekker likes this.