Player is not moving using on-screen button in android but working fine in Game/Simulator mode

Hi,
I’ve a very simple scene with a player, a UI button & event system. I’ve attached an “On-screen button” component to the UI button which simulates Keyboard’s UP key which I’ve used in script to move the player forward.
When I click that button in Game mode or Simulator mode it moves the button forward.

What I have tried?

  • I have the Active Input Handling in Player settings set to “Both” and tried only New Input System as well.
  • I have tried creating a New InputActions.
  • I have tried creating a completely new unity project from scratch with just these basic things
  • I have tried to attach “on screen button” component to both UI Button as well as UI Image.
  • I have tried mapping the button to keyboard “W” key as well. Note: While in game mode my keyboards Up/W key are making the player move.

Here are all the GameObjects for reference

Observations

  • The UI button when clicked on the android device changes color to the buttonPressed color. That means, the button click is getting registered.

I’ve tried finding a solution to this greatly and now I’m getting frustrated with it. Please provide some solution or alternatives I can use. This also doesn’t seem like a new solution to solve for. It’s a basic function in any game and youtube tutorials only show the games working in Game Mode and not in actual mobile device.

Hello, I’m having the same issue. Are there any improvements on this?

Thanks to the sample project Lost Cyrpt, I solved it.

Before writing my solution, I should state that I’m still new to Unity; so, there may be some inefficient or wrong applications (but it works, for now).

Previously I was using Invoke Unity Events behavior of PlayerInput and was reading input like this:

private float movementInput = 0;
public void OnMovementChanged(InputAction.CallbackContext context)
{
    movementInput = context.ReadValue<float>();
}

And changed it to:

private float movementInput = 0;
void Update()
{
    var keyboard = Keyboard.current;
    if (keyboard.dKey.isPressed)
    {
        movementInput = 1;
    }
    else if (keyboard.aKey.isPressed)
    {
        movementInput = -1;
    }
    else movementInput = 0;
}

My movement buttons:

I only need the horizontal input in my project, that’s why my movementInput is a float instead of a vector. But, even if you need a more-than-1-dimensional input, I think you can make it work with a few more if statements.

Note that, if you don’t want to perform the same action multiple times (jumping or using a skill, for example), you should use keyboard.jKey.wasPressedThisFrame instead of keyboard.jKey.wasPressed (can obviously be another key than jKey)