我正在尝试使用我的电话中的接近传感器使用统一遥控器。我试着跟踪这个文档
输入系统工作正常。我已经安装了所有的设备,我已经导入了所有的示例,同时,统一遥控器也在工作,我已经使用传感器测试应用程序在我的android设备上测试了传感器。
我想要做的/All是触发这个脚本,并测试它是否工作。我无法将它附加到gameObject,因为它不是从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获得null值。我想要做的是移动一个gameObject时,接近传感器被触发(用户是拿着手机对他们的耳朵)。
如能提供任何帮助,将不胜感激。
发布于 2021-11-03 17:52:29
您正在获得“邻近错误”,因为显示它的代码位于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");
}
}https://stackoverflow.com/questions/69797813
复制相似问题