If you're reading this, it's probably because you played Smash Peter With Mobile and want to see how to use motion sensing in a mobile game, so without further ado here's the JavaScript code that I originally found could get the accelerometer info if the phone's browser lets it be available. (I'm still not sure how many phones and browsers allow it though, and haven't found any way to get Firefox to read sensors, so also include other ways for the player to interact with your game.)
<html> <body bgcolor="white"> <p id="para">DeviceMotionEvent is not supported by this browser</p> <button id="reqPerm" onclick="requestPerm()">Grant permission to use sensors</button> <script> let html_para = document.getElementById("para"); let motionOutput = "No motion data received yet"; function requestPerm() { DeviceMotionEvent.requestPermission(); } window.addEventListener('devicemotion', function(event) { motionOutput = "Acceleration along the X-axis: " + event.acceleration.x + "<br>" + "Acceleration along the Y-axis: " + event.acceleration.y + "<br>" + "Acceleration along the Z-axis: " + event.acceleration.z + "<br><br>" + "Rotation along the X-axis: " + event.rotationRate.x + "<br>" + "Rotation along the Y-axis: " + event.rotationRate.y + "<br>" + "Rotation along the Z-axis: " + event.rotationRate.z }); function updateFrame() { html_para.innerHTML = motionOutput; requestAnimationFrame(updateFrame); } updateFrame(); </script> </body> </html>
Importantly, for Apple devices you need to include a request to use the sensors, and it needs to be made from within a callback from a button press. It can be done with the function DeviceMotionEvent.requestPermission(); which only works on Apple devices that support it. If the device doesn't support it, it won't crash, but it will throw an error to the console. So if you don't want to see it throwing a console error you can do this
if(DeviceMotionEvent.requestPermission) {DeviceMotionEvent.requestPermission();}
In practice my phone and probably most phones don't have gyroscopes or won't let you read them so event.rotationRate will probably not be read, but there is also event.accelerationIncludingGravity which was useful as a way to convert the raw acceleration data from the phone to more useful values -- apparently not all phones report acceleration using the same units, but you can tell how many units gravity is generating and use that as a reference, which I did in Smash Peter.
And if you're a Godot dev, here's the actual source code of the AutoLoad script from Smash Peter that listens to the phone's accelerometer data and lets you use it in-game. Remember that request_permission() needs to be called from within a button click callback (I tried using it in an InputEvent handler checking if the event is an InputEventMouseButton and that didn't work, but making it be from a button's "pressed" event did work).
extends Node var device_accel:Vector3 = Vector3.ZERO var device_accel_including_gravity:Vector3 = Vector3.ZERO var _javascript_callback func _ready(): if OS.has_feature("JavaScript"): _javascript_callback = JavaScript.create_callback(self, "_on_device_motion") JavaScript.get_interface("window").addEventListener("devicemotion", _javascript_callback) func _on_device_motion(event): device_accel.x = event[0].acceleration.x device_accel.y = event[0].acceleration.y device_accel.z = event[0].acceleration.z device_accel_including_gravity.x = event[0].accelerationIncludingGravity.x device_accel_including_gravity.y = event[0].accelerationIncludingGravity.y device_accel_including_gravity.z = event[0].accelerationIncludingGravity.z func request_permission(): JavaScript.eval("if(DeviceMotionEvent.requestPermission) {DeviceMotionEvent.requestPermission();}")