首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在联合中触发接近传感器的问题

在联合中触发接近传感器的问题
EN

Stack Overflow用户
提问于 2021-11-01 13:44:01
回答 1查看 296关注 0票数 0

我正在尝试使用我的电话中的接近传感器使用统一遥控器。我试着跟踪这个文档

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

我想要做的/All是触发这个脚本,并测试它是否工作。我无法将它附加到gameObject,因为它不是从MonoBehaviour派生的。

编辑:我已经将脚本更改为MonoBehaviour.before,它是从传感器派生的

这是我的新传感器脚本:

代码语言:javascript
复制
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获得null值。我想要做的是移动一个gameObject时,接近传感器被触发(用户是拿着手机对他们的耳朵)。

如能提供任何帮助,将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-03 17:52:29

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

代码语言:javascript
复制
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");
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69797813

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档