Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Siggytron · Nov 22, 2020 at 06:26 AM · sensor

Input System 1.0.0 Sensors - How to get data? [Solved]

I decided to try out the Input System 1.0.0 because you can apparently get sensor input from a phone's sensors.

The quick start guide gives this example code (https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/QuickStartGuide.html):

 using UnityEngine;
 using UnityEngine.InputSystem;
 
 public class MyPlayerScript : MonoBehaviour
 {
     void FixedUpdate()
     {
         var gamepad = Gamepad.current;
         if (gamepad == null)
             return; // No gamepad connected.
 
         if (gamepad.rightTrigger.wasPressedThisFrame)
         {
             // 'Use' code here
         }
 
         Vector2 move = gamepad.leftStick.ReadValue();
         // 'Move' code here
     }
 }

And then the sensor support page gives this code (https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Sensors.html):

 InputSystem.EnableDevice(Gyroscope.current);
 if (Gyroscope.current.enabled)
     Debug.Log("Gyroscope is enabled");

So I tried a basic script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.InputSystem;
 
 public class Sensors : MonoBehaviour
 {
     void Start()
     {
         InputSystem.EnableDevice(AmbientTemperatureSensor.current);
     }
     void Update()
     {
         if (AmbientTemperatureSensor.current.enabled)
         {
             Debug.Log ("AmbientTemperatureSensor is enabled");
         }
     }
 }

But I get this error

NullReferenceException: Object reference not set to an instance of an object Sensors.Update () (at --- the if (AmbientTemperatureSensor.current.enabled) line)


Based on the fix given by @andrew-lukasik and a comment by @Llama_w_2Ls , I modified my code. Here is the final. I've also posted a screenshot of the screen. As you can see, my particular phone -- a Motorola Android phone -- does not have an ambient temperature sensor but DOES have an accelerometer.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.InputSystem;
 using TMPro;
 
 public class Sensors : MonoBehaviour
 {
     public TextMeshPro texttemp;
     public TextMeshPro textaccel;
     AmbientTemperatureSensor ambient;
     Accelerometer accelerometer;
 
     void Start()
     {
         ambient = AmbientTemperatureSensor.current;
         accelerometer = Accelerometer.current;
     }
 
     void Update()
     {
 
         if (ambient != null)
         {
             InputSystem.EnableDevice(ambient);
             textaccel.text = "enabled";
         }
         else
         {
             texttemp.text = "null";
         }
 
         if (accelerometer != null)
         {
             InputSystem.EnableDevice(accelerometer);
             textaccel.text = "enabled";
         }
         else
         {
             textaccel.text = "null";
 
         }
 
     }
 }

Sensor Readout

sensorreadout.jpg (468.7 kB)
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andrew-lukasik · Nov 22, 2020 at 10:46 AM 2
Share
 var sensor = AmbientTemperatureSensor.current;
 if( sensor!=null )
 {
     /* read values here */
 }
 else
 {
     // no sensor connected | no access permission
 }
avatar image Siggytron andrew-lukasik · Nov 22, 2020 at 08:40 PM 0
Share

Thanks. That worked.

avatar image Llama_w_2Ls · Nov 22, 2020 at 04:06 PM 0
Share

$$anonymous$$ost mobile devices dont have Temperature sensors (in my findings). There are great sensor apps on the playstore to see how each sensor works and if you have it or not.

avatar image Siggytron Llama_w_2Ls · Nov 22, 2020 at 07:47 PM 0
Share

I think you're right. I picked that sensor kind of arbitrarily. I can try a different sensor, perhaps accelerometer since I know all phones have that. However, I wanted to start with how to basically get data from a phone's built-in sensor.

avatar image Llama_w_2Ls Siggytron · Nov 24, 2020 at 04:29 PM 0
Share

You'll have to see if the sensor is built into Unity's Input class, for example Input.Gyroscrope or Input.Accelerometer etc. If not, there's an asset online called Gyro-Droid, which claims to support all sensors for your mobile device inside of Unity, but it is a bit pricy.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

137 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

sensor temperature android 0 Answers

Arduino Temperature Sensor Read Values in Unity 5 Answers

Get Polar H-10 heart rate monitor data to Unity 1 Answer

how to connect a Phidget Spatial 0/0/3 to Unity? 1 Answer

Cannot get value from sensor as Android background service 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges