How to use InputSystem.StepCounter

Hello, I would like to count the user’s real world steps in my mobile game but I’m having a hard time finding anything on the step counter in Unity’s InputSystem. It seems there are no tutorials on this, and most people seem to give up and use third party systems. I’ve been left trying to pillage code off the forums, but it doesn’t really work. And the unity documentation is sorely lacking examples.

Here is what I have so far. The first block is, I think, intended to check if we have the step counter permissions on Android. I don’t know how to go about this on other platforms. this doesn’t seem to work in tethered mode, as I don’t get a popup.

private void Start()
{
    #if PLATFORM_ANDROID
    if (!Permission.HasUserAuthorizedPermission("android.permission.ACTIVITY_RECOGNITION"))
    {
        Permission.RequestUserPermission("android.permission.ACTIVITY_RECOGNITION");
    }
    #endif
}

I am then trying to activate the counter using:

if (!Application.isEditor) { InputSystem.EnableDevice(StepCounter.current); }

And I am then trying to get it to print the steps using this. I’m not getting anything except my default text, so I assume this never fires. Even though I shook it until the cable fell out :stuck_out_tongue:

    void FixedUpdate()
    {
        if (StepCounter.current.enabled)
        {
            counter[3].text = StepCounter.current.stepCounter.ReadValue().ToString();
        }
    }

There‘s also the script API side of the manual: Class StepCounter | Input System | 1.8.2

It mentions that „noisy input“ may not automatically get activated, so you may have to call MakeCurrent() explicitly. Furthermore it shows that there are Android and iOS specific variants of the StepCounter. Not sure if StepCounter uses these behind the scenes or whether you are supposed to explicitly use either of these.

In any case, input updates go into Update not FixedUpdate. And for testing, attach the debugger and seta breakpoint so you can see exactly what‘s going on and each variable’s values rather than having to rely on debug GUI output which may have its own bugs.

Thanks a lot, though I couldn’t find any examples of step counter code on that page. Honestly, I really need a working example of how to use the code and I can’t seem to find one on the page. By the way, would ‘Active Input Handling’ affect it? The system says that Android cannot handle it being set to ‘both’, but switching it to ‘new’ disables all the buttons on Android - so I don’t think that’s true.
9766170--1399095--upload_2024-4-12_10-41-8.png

As far as I can tell, the step counter is just always returning 0 on Android.