功能:通过按右手柄a、b按键,实现camera(头盔)上下移动
1.创建XR InputActionReference
依次点击Right Interaction->Action 中+号
手柄上各个按键对应的名称:
转自:https://blog.youkuaiyun.com/weixin_49076427/article/details/135676865
2.编辑脚本
public class MoveUpDown : MonoBehaviour
{
public InputActionReference moveUpAction;
public InputActionReference moveDownAction;
private float _moveSpeed = 0.05f;//移动速度
private void OnEnable()
{
//启动输入监听
moveUpAction.action.Enable();
moveDownAction.action.Enable();
}
private void OnDisable()
{
//禁用输入监听
moveUpAction.action.Disable();
moveDownAction.action.Disable();
}
// Update is called once per frame
void Update()
{
//如果b按钮被按下
if (moveUpAction.action.IsPressed())
{
Debug.LogWarning("b按钮被按下");
Move(Vector3.up);
}
if(moveDownAction.action.IsPressed())
{
Debug.LogWarning("a按钮被按下");
Move(Vector3.down);
}
}
private void Move(Vector3 direction)
{
transform.position += direction * _moveSpeed ;
}
}
3.绑定脚本
将脚本绑定到Camera上,并且拖入创建的InputActionReference(在XRI中找)