Search Unity

Is There a Way to Use Input.acceleration on WebGL?

Discussion in 'Web' started by bunnyUFO, Jul 29, 2021.

  1. bunnyUFO

    bunnyUFO

    Joined:
    Dec 23, 2017
    Posts:
    5
    Has anyone gotten Input.acceleration to work on WebGL build?
    Is there anything specific that needs to be done to enable it on WebGL?

    I have a mobile game that uses Input.acceleration for movement and wanted to also be able to host it on webGL too. When I host it on WebGL it does not seem to be able to access Input.acceleration, and as a result my game has no way to move main character.

    The alternative is to use javascript on the hosted page to get the device acceleration/gyroscope data and feed it to unity canvas using a plugin. Avoiding that for now because it seems hacky and more work than trying to get unity library to work on WebGlL.

    Hopefully someone else has gotten it to work and can help me out.
     
    Last edited: Jul 29, 2021
    tantx and makaka-org like this.
  2. brendanduncan_u3d

    brendanduncan_u3d

    Unity Technologies

    Joined:
    Jul 30, 2019
    Posts:
    437
    We added acceleration/gyroscope support to 2021.2, as part of our mobile push. Unfortunately it will unlikely be back-ported to early versions, being a new feature.
     
    tantx and makaka-org like this.
  3. bunnyUFO

    bunnyUFO

    Joined:
    Dec 23, 2017
    Posts:
    5
    Awesome! I'll get my project updated to the 2021.2 pre-release to test it out. Using 2021.1.14 now.
     
  4. hookmanuk

    hookmanuk

    Joined:
    May 28, 2013
    Posts:
    20
    I just updated to the latest 2021.2.0b8 but Input.Acceleration.x appears to always be 0 for me. Is there anything else I need to set in order for it to work? Maybe permissions?

    I'm running this code to update a text object, which does update, but always to 0,0 on my Android phone running on a webGL build hosted via itch.io

    Code (CSharp):
    1. public class UpdateCamera : MonoBehaviour
    2. {
    3.     public TextMesh DebugText;
    4.  
    5.     // Update is called once per frame
    6.     void Update()
    7.     {      
    8.         DebugText.text = "x: " + Input.acceleration.x.ToString() + ", y: " + Input.acceleration.y.ToString();
    9.     }
    10. }
     
    tantx likes this.
  5. kimvasquez17

    kimvasquez17

    Joined:
    Dec 19, 2017
    Posts:
    22
    I do have the same problem. Do you have the solution already?
     
    tantx likes this.
  6. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,166
    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):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3.  
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6.  
    7. public class TTTT : MonoBehaviour
    8. {
    9.     void Start()
    10.     {
    11.         if(AttitudeSensor.current != null)
    12.             InputSystem.EnableDevice(AttitudeSensor.current);
    13.     }
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         if(AttitudeSensor.current != null)
    18.             gameObject.transform.rotation = AttitudeSensor.current.attitude.ReadValue();
    19.         else gameObject.transform.rotation = Quaternion.Euler(0,(float)System.DateTimeOffset.Now.TimeOfDay.Milliseconds * 360 / 1000f,0);
    20.     }
    21. }
    22.  
     
    tantx, makaka-org, bunnyUFO and 2 others like this.
  7. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    547
    If you need gyroscope/accelerometer support for webgl, my plugin covers it.
     
    makaka-org likes this.
  8. bunnyUFO

    bunnyUFO

    Joined:
    Dec 23, 2017
    Posts:
    5
    I did not get it to work with 2021 version had put thge WebGL demo on hold.

    Just checked up ont his thread, will try version 2022.1.6f1 or 2022.2.0a17 and follow @Thaina code suggestion.

    Thanks for the help everyone!
     
    tantx likes this.
  9. bunnyUFO

    bunnyUFO

    Joined:
    Dec 23, 2017
    Posts:
    5
    I'm starting to think the issue is my phone.
    Not sure why AttitudeSensor.current is always null, even tried a native andorid build.
    Would probably wok on other phones.


    It's just an old project I wanted to add to a portfolio site, but works for PC anyway.
     
  10. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,166
    Forgot to mention in new input system there is many config need to do such as this

    Not sure it would help anything

    upload_2022-11-23_12-44-44.png
     
    tantx and makaka-org like this.
  11. alexis_morin

    alexis_morin

    Joined:
    Jan 13, 2017
    Posts:
    6
    Logging in to give a heads up to anybody who bumped their head against this;

    If you're trying to upload your game onto Itch.io and testing it there; this might be the cause of your woes; something about the way the content is wrapped (at least with the default WebGL template, didn't try with the minimal one) on itch pages causes deviced sensors not to pick up/update, even if you call the javascript methods directly. I would reccomend trying a local build running a local server set up with something like Servez.

    For my usecase I'm not using itch as a host, so fwiw I also got it to work with Unity 2021.2 and the new input system. A progress path might be trying the minimal template to see if the WebGL is able to access the data then.
     
  12. bunnyUFO

    bunnyUFO

    Joined:
    Dec 23, 2017
    Posts:
    5
    It should work on WenlnGL builds, but you need to ask for access to sensors in a different way. Got it to work once but forgot how.


    Anyway, I was eventually able to get the input to work on my Android device. However, game was not working. Inspected values on logs by addingns temporary UI to display sensor values.

    Sensor input worked, but the value range was not compatible with the code in that old project. It didn't seem worth the effort to fix it anymore and am okay with only having a PC version.

    Thanks for the suggestions, might consider making a new game from scratch wirh guro input, but not worth fixing this one.
     
    Last edited: Mar 10, 2023
    tantx likes this.
  13. makaka-org

    makaka-org

    Joined:
    Dec 1, 2013
    Posts:
    1,024
    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 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.
     
    Marks4 likes this.
  14. tantx

    tantx

    Joined:
    Jul 5, 2017
    Posts:
    23
    Thank you so much for your help in here and another similar topic, really helps me so much( include let me know new input system could be work.)
     
  15. tantx

    tantx

    Joined:
    Jul 5, 2017
    Posts:
    23
    I have similar issue before
    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 enable the device and call it again when user click start button.
    Code (CSharp):
    1. if (LinearAccelerationSensor.current != null)
    2. 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,so wired
    It seems some thing happened When I out and return to the game

    Do someone know is there any method to let this thing happen directly when I just enter my game? How you get your sensor read value works on android+Chrome for your itch game?
     
    Last edited: Jul 5, 2023
  16. tantx

    tantx

    Joined:
    Jul 5, 2017
    Posts:
    23
    my Hero, Hope to know, have you thought of any idea/suggest for the situation which I mentioned in above #15 ? I really confused of this problem, I think maybe you have some experience about this, so much appreciate!
     
  17. tantx

    tantx

    Joined:
    Jul 5, 2017
    Posts:
    23
    Can you remember how you did it before?
    I get some stuck when android+chrome to my itch game
    The situation I have just wrote down at the post #15 above, I found that read value from sensor not work directly when game just entered on itch.

    Do you have any idea or experience about this situation?
    Any help would be appreciated!
     
    Last edited: Jul 5, 2023
  18. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,166
    I don't know. If it's me I would start on checking that is the EnableDevice was being called properly. Maybe it need to wait for checking permission and so it need to be delayed somehow
     
    tantx likes this.
  19. tantx

    tantx

    Joined:
    Jul 5, 2017
    Posts:
    23
    Thank you for your reply and nice suggestion
    I just found after I clicked the camera permission hint bubble, the sensor read value could work

    hummm... it seems the camera permission call blocked another call?... or just maybe the bubble click action is same meanning with exit and restore.
    I will do more experiments on this.