I’ve been trying for two weeks to get multiple different mobile phone step counter plugins, addons, and so on working to simply detect user steps on mobile (testing on Android). So far I haven’t counted a single step, despite trying with a great many.
EDIT I WORKED IT OUT!!
Here is how to count steps in Unity for Android! You can adapt this for IOS.
Note that “counting steps” on mobile is a crud. It’s merely counting certain acceleration thresholds with pauses in between them which means it could be steps, or it could be someone waving with the phone, or it could be driving on a bumpy road or on a bike, or it could be the phone on its last stretches in the washing machine.
That’s fair enough, but it doesn’t need to be super exact. It just needs to be ballpark. Any idea how to use InputSystem to count that? There is a step counter sensor, but it always returns zero regardless of if the app is open. I think it’s a permissions issue, but I have no way to ask.
I also don’t know if Unity loading the app onto my phone would silence something like that.
Another problem is that I have input handling set to ‘both’. Unity keeps telling me mobile phones can’t handle this, but if I set it to ‘old’ my buttons stop working. On new it just goes to both.
If so, the Android SDK should provide details. You can enter what permissions the app requires in Player Settings.
There’s also a possibility that StepCounter isn’t returning values until a certain threshold of steps has been taken according to this thread: https://discussions.unity.com/t/841471
Thanks a lot. Since I never had it ask me, I assume the permissions thing. So I would just need to preset Player Settings to demand them at startup to get permission? And you are sure I can use Player Input rather than needing some third party App?
Can I use player input to keep track of the steps via a notification while the app is minimised? Or would it simply keep track of them, then update once the app was maximised?
It’s been a while but I remember the “required app permissions” go in a Player Settings field. I believe this gets then added to the manifest which tells the device which permission prompt to show or what to show in it.
It’s possible that additional steps are required. I was mainly doing iOS many years ago and if I recall correctly (and the process hasn’t changed) you also had to set these permissions in the developer dashboard. But this could have been just one of the steps before review submission.
I’m not sure if StepCounter is all it takes. Since the source code is available, I’d check what it actually does or try to set breakpoints and see if the package code is running that will eventually lead to StepCounter changes or if it doesn’t where and why this happens.
Last time I checked there was or is no mobile background processing in Unity, I doubt Input System does that. Typically you’d have some native background processing code running and it may be possible to call into your own C# methods but this wouldn’t be helping if StepCounter never changes while in the background.
Ah, I see. So you don’t know how to modify player preffs to do that? I wish there was some sort of tutorial on this. I may have to simply abandon the project if I can’t get the features I need. Which is frustrating because I expect the answer is actually really simple. Unfortunately, I get the impression nobody develops unity for mobile devices. At least not using the sensors.
Yes, as in making a native plugin that utilizes the native step counter API and also runs in the background, and somehow makes that data available to the Unity app.
That’s the big use-case by far. About two thirds of all mobile games are made with Unity according to data about two years back.