【问题标题】:Having issues triggering the proximity sensor in Unity在 Unity 中触发接近传感器时出现问题
【发布时间】:2021-12-16 05:40:54
【问题描述】:

我正在尝试使用统一遥控器在我的手机中使用接近传感器。 我试着关注这个Documentation

输入系统工作正常。我已经安装了所有东西,我已经导入了所有样本,统一遥控器也在工作,我已经使用传感器测试应用程序在我的 android 设备上测试了传感器。

/我想要做的就是触发这个脚本并测试它是否正常工作。我无法将它附加到游戏对象,因为它不是从 MonoBehaviour 派生的。/

编辑:我已将脚本更改为 MonoBehaviour.before,它源自传感器

这是我的新传感器脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;

public class Prox : MonoBehaviour
{
    public ProximitySensor proximity;

private void Start()
{

    if (ProximitySensor.current != null)
    {
        var _ProximitySensor = ProximitySensor.current;
        InputSystem.EnableDevice(_ProximitySensor);
        Debug.Log("proximity sensor is working");
    }
    Debug.Log("proximity error");
}
void Update()
{

    if (proximity != null)
    {
      //  InputSystem.EnableDevice(proximity);
        var _ProximitySensor = ProximitySensor.current;
        Debug.Log(_ProximitySensor);
        var _dist = _ProximitySensor.distance;
        Debug.Log(_dist);
    }
    else
    {
       Debug.Log("null");
    }
}
}

控制台的输出总是返回“接近错误” 这意味着我从 ProximitySensor.current 得到空值。 我想要做的是在触发接近传感器时移动游戏对象(用户将手机放在耳边)。

任何帮助将不胜感激

【问题讨论】:

  • 请使用正确的标签!请注意,unityscript 是或更好的是曾经是一种 JavaScript 风格,类似于早期 Unity 版本中使用的自定义语言,并且现在已经不推荐使用了!你的代码显然是c# ...
  • 谢谢你的澄清,我不知道

标签: c# android unity3d sensors proximitysensor


【解决方案1】:

您收到“接近错误”,因为显示它的代码位于 Start() 函数中,并且将始终执行。你必须写:

private void Start()
{
    if (ProximitySensor.current != null)
    {
        var _ProximitySensor = ProximitySensor.current;
        InputSystem.EnableDevice(_ProximitySensor);
        Debug.Log("proximity sensor is working");
    }
    else
    {
        Debug.Log("proximity error");
    }
}

【讨论】:

  • 此函数返回接近错误,传感器不工作。我不知道为什么会这样,我已经用传感器测试应用程序测试了我的 android 设备中的传感器
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-09
  • 2011-07-14
  • 1970-01-01
相关资源
最近更新 更多