- Home /
How to get sensor values with the Input System?
I am trying to get the distance value from the proximity sensor by using unity's Input System but nothing that I tried seems to work. I tried looking into the documentation, there it says something about "AxisControl" but I have no clue how to code it to work. https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/api/UnityEngine.InputSystem.ProximitySensor.html
This my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using TMPro;
using System;
public class Sensors : MonoBehaviour
{
public TMP_Text textprox;
public ProximitySensor proximity;
void Start()
{
proximity = ProximitySensor.current;
}
void Update()
{
if(proximity != null)
{
InputSystem.EnableDevice(proximity);
var _ProximitySensor = ProximitySensor.current;
var _dist = _ProximitySensor.distance;
textprox.text = "Proxy: " + _dist;
}
else
{
textprox.text = "null";
}
}
}
I haven't used sensors before but something about your code stands out. In your Start()
you're setting proximity
as the current input, then in your Update()
you're enabling proximity
, but setting _ProximitySensor
as the current input and getting the distance from that when it hasn't been enabled. I may just be ignorant of the way this works but that seems rather wrong to me. Wouldn't you wanna get the distance from the enabled device?
Answer by Roman_Keivan · Sep 05, 2021 at 10:42 PM
@pandagamingroyt > Try this
public float ProximityDistanceSensor(bool m_isActiveProxSens, float m_frequency = 16f)
{
proximitySensor.samplingFrequency = m_frequency; // Set sampling frequency of proximitySensor to sample 16 times per second.
if (m_isActiveProxSens)
{
InputSystem.EnableDevice(proximitySensor);
Debug.Log("Proximity sensor is: " + proximitySensor.enabled);
}
else
{
InputSystem.DisableDevice(proximitySensor);
Debug.Log("Proximity sensor is: " + proximitySensor.enabled);
}
return actionControlls.UI.DistanceActionSensor.ReadValue<float>();
}
your code is not working. actionControlls is not declared and does not exist can you fix this please I really need to work on this sensor
Your answer
Follow this Question
Related Questions
C# HUD axis button display 0 Answers
Input for bluetooth controller? 0 Answers
I cant change Input axis 0 Answers
U3DXT iOS plugin proximity sensor causing screen to go black? 0 Answers
Does unity support a 16 bits axis input? 0 Answers