I've seen non-Unity WebGL things that support the accelerometer, so... maybe? But considering we only just got touch input, and mobile is not yet supported in Unity WebGL, it's unlikely and you're probably best off avoiding working with WebGL for mobile entirely at the moment. Edit: if you can get it to even run on your mobile, just try these out: http://docs.unity3d.com/ScriptReference/SystemInfo-supportsAccelerometer.html http://docs.unity3d.com/ScriptReference/Input-acceleration.html
On which platforms? The last time I checked, WebGL builds weren't supported at all on either Android or iOS.
They aren't supported. But that said, they may still work, depending on your project and device used, and in that case, 5.1 should make Input.acceleration work Given the unsupported status, this is probably not that relevant right now (but the thread starter was asking about it, so..) - but I'm sure it will become more relevant in the future.
Hmmm here from Unity 2021.1.9f1. 1. Work a charm on straight iOS build. 2. However WebGL iOS is not working. I debug the Input.Acceleration from the iPhone 11 Safari to the Mac Safari > Develop Console and it always report (0,0). 3. Test on WebGL-Android is working but slowly and to the opposite of what I had on the iOS build... (I mean it's like the X axis is flipped ) Still not support after 6 years?...
Unity WebGL doesn't officially support Mobile. Never had. They only -started- adding support in the latest 2021.2 beta.
Alright then I just tried a WebGL build using 2021.2.0b9.3335 and the Input.Acceleration is still not supported for iOS.
Finally did some research and got it works. Tested on iphone 11. 1. A nice page which illustrate everything you can grab from the device of Javascript : - https://sensor-js.xyz/demo.html 2. checking the code you'll find some nice documentation about it with a nice simple lines of code with Javascript: - https://developer.mozilla.org/en-US/docs/Web/API/DeviceMotionEvent Code (csharp): window.addEventListener('devicemotion', function(event) { console.log(event.acceleration.x + ' m/s2'); }); 3. Be sure your page url begin is https:// to prompt the Apple Device Motion User access. If http:// only, it won't show it and the device motion is muted. 4. Unfortunately SendMessage cannot return a Vector3 then you'll have to add a listener for each axis. 5. Below the result of my research to access the Acceleration.x only. you can pretty much do the same for every single sensort you can see from the 1st url I shared. Unity C# : Code (csharp): public class WebGLInputAcceleration : MonoBehaviour { [DllImport("__Internal")] private static extern void AddAccelerationEventX(string objSource, string msg); [DllImport("__Internal")] private static extern void RemoveInputAccelerationEventX(); public void EnableAccelerationDetection() { AddAccelerationEventX(gameObject.name, "MsgSimulationResult"); } public void DisableAccelerationDetection() { RemoveInputAccelerationEventX(); } public void MsgSimulationResult(float value) { Debug.Log( "Acceleration.<color=red>x</color> : " + value); } } InputAcceleration.jslib to drop to your Plugins directory : Code (csharp): mergeInto(LibraryManager.library, { AddAccelerationEventX: function (objSource, msg) { AddAccelerationEventX(Pointer_stringify(objSource), Pointer_stringify(msg)) }, RemoveInputAccelerationEventX: function () { RemoveInputAccelerationEventX(); } }); InputAcceleration.js to include to your index.html : Code (csharp): var eventAccelerationX = null; function AddAccelerationEventX(objSource, msg) { if(eventAccelerationX == undefined) { eventAccelerationX = function(event) { //console.log(event.acceleration.x + ' m/s2'); unityInstance.SendMessage(objSource, msg, event.acceleration.x); } } window.addEventListener('devicemotion', eventAccelerationX); } function RemoveInputAccelerationEventX() { if (eventAccelerationX != undefined) { window.removeEventListener('devicemotion', eventAccelerationX); } }
I actually forgotten a very important point before dealing with the device motion especially on iOS... ...do not forget to prompt for devicemotion permission : - https://dev.to/li/how-to-requestper...n-and-deviceorientation-events-in-ios-13-46g2
I can get it work in 2022 (not try 2021) but only with new input system. Old input system (Input.gyro.attitude and all other) are not working Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; public class TTTT : MonoBehaviour { void Start() { if(AttitudeSensor.current != null) InputSystem.EnableDevice(AttitudeSensor.current); } // Update is called once per frame void Update() { if(AttitudeSensor.current != null) gameObject.transform.rotation = AttitudeSensor.current.attitude.ReadValue(); else gameObject.transform.rotation = Quaternion.Euler(0,(float)System.DateTimeOffset.Now.TimeOfDay.Milliseconds * 360 / 1000f,0); } }
I spended a lot of time trying to get the accelerometer sensor on iPhone via WebGL work. Finally I saw the ultimative pointer in the console log on iPhone: "requesting device motion access requires a user gesture to prompt". It means that the enabling of sensor, which includes the requsting of device access, must be triggered by a user interaction. In my game I linked the initializing of the accelerometer just to "Start"-Button of the game: Code (CSharp): public void ClickStart(int buttonNo) { InputSystem.EnableDevice(Accelerometer.current); } Otherwise: it wouldn't be working as a part of "Start" or "Awake" lifecycle functions, because they are not directly triggered by the user.
I created a separate thread with Unity Assets that support all sensors and modules for WebGL: gyroscope, accelerometer, GPS, camera, vibration... Current list of Assets: Gyroscope Accelerometer WebGL — enables you to read the gyroscope and accelerometer information provided by the browser. Geolocation WebGL — allows you to access GPS on the web. Device Camera WebGL - allows you to access the camera feed and stream its contents into a render texture (or Texture2D). It works on Chrome, Safari, Firefox, Opera, Edge. Vibration WebGL - enables you to vibrate your mobile phone. You can control how long you want to keep the mobile vibrating, as well as define a vibration sequence. Screen Orientation WebGL — a way to listen to device orientation changes.
Hi, Your answer helps me a lot! Now I found that my webGL build could work stable for IOS on itch.io , when I just entered, it even shows the permission access call hint, LinearAccelerationSensor get value after that. But when I use Andriod + Chrome to visit my link, I found there no any permission call hint, even I already do Code (CSharp): if (LinearAccelerationSensor.current != null) InputSystem.EnableDevice(LinearAccelerationSensor.current); The LinearAccelerationSensor.read() is no value, But this not all the time, If I do the operation below: * back to outside page( the game page on itch before I enter game start) * then come back to my game by [Restore Game]Button , it would finally work! every senor would get value, even the old input system, like Input.gyro.userAcceleration... I don't know why It seems some thing happened When I out and return to the game Do you know is there some method to let this happen directly when I just enter my game? Any help would be appreciated!