简单位置控制教程
本教程演示如何在 Unity 场景中,借助动态游戏对象控制 Inverse3 设备的光标位置,内容基于 入门指南 中介绍的概念。
简介
Inverse3 设备支持两种控制模式:力控制和位置控制。
前者根据光标位置调节力的大小,后者则直接操控光标的位置。
本教程沿用 动态场景中的力反馈教程 中的场景设置,重点在于在 FixedUpdate 方法中使用 CursorSetPosition,使设备光标能够跟随移动的游戏对象。
场景设置
请先按照 动态场景中的力反馈教程 中所述的场景配置进行设置,其中包括一个 Haptic Rig 以及由 MovableObject 组件控制的 Moving Ball 游戏对象。
实现 SpherePositionControl 组件
将 Moving Ball 游戏对象上的 SphereForceFeedback 组件替换为一个名为 SpherePositionControl.cs 的新 C# 脚本。
在 SpherePositionControl 类中定义以下属性:
public Inverse3 inverse3;
public float minSyncDistance = 0.05f;
private bool _isCursorSynchronized;
- inverse3:对 Inverse3 设备组件的引用,通过 Inspector 设置。
- minSyncDistance:启动光标同步所需的最小距离阈值。
- _isCursorSynchronized:标志位,指示光标的移动是否与 Moving Ball 同步。
同步逻辑
实现根据与 Moving Ball 的接近程度来启动和停止光标同步的方法:
private void StartSynchronizeCursor()
{
var cursorPosition = inverse3.Cursor.transform.position;
GetComponent<MovableObject>().SetTargetPosition(cursorPosition, teleport: true);
_isCursorSynchronized = true;
}
private void StopSynchronizeCursor()
{
_isCursorSynchronized = !inverse3.Release();
}
这里使用
MovableObject.SetTargetPosition(position, teleport:true),它会将 MovingBall 瞬移到光标位置。
Update 与 FixedUpdate 方法
在 Update 方法中,根据光标与 Moving Ball 之间的距离切换光标同步状态:
private void Update()
{
var distance = Vector3.Distance(inverse3.CursorPosition, transform.position);
if (!_isCursorSynchronized && distance <= minSyncDistance)
{
StartSynchronizeCursor();
}
else if (_isCursorSynchronized && distance > minSyncDistance)
{
StopSynchronizeCursor();
}
}
在 FixedUpdate 中,若同步处于激活状态,则更新光标位置以与 Moving Ball 保持一致:
private void FixedUpdate()
{
if (_isCursorSynchronized)
{
inverse3.SetCursorPosition(transform.position);
}
}
使用 FixedUpdate 方法可确保位置控制以稳定的帧率运行,不受图形渲染性能的影响。
初始化
在 Awake() 中,无论左右手配置如何,都要确保 Moving Ball 的起始位置位于 Inverse3 设备可触及的范围内:
private void Awake()
{
inverse3.Ready.AddListener(device =>
{
GetComponent<MovableObject>().SetTargetPosition(((Inverse3)device).WorkspaceCenterLocalPosition, teleport: true);
});
}
游戏体验
请固定好 Inverse3 设备,并确保其周围有充足的空间。 激活播放模式(Play Mode)并将光标移近 Moving Ball,Inverse3 便会跟随 Moving Ball 的运动。 可以使用键盘输入移动 Moving Ball,以展示光标追踪其位置的能力。

源文件
本教程的完整场景及相关文件可在 Unity Package Manager 中的 Tutorials 示例中找到。
其中包含 MovableObject 脚本,该脚本在多个示例中被用于通过键盘输入控制游戏对象的移动。
SpherePositionControl.cs
/*
* Copyright 2024 Haply Robotics Inc. All rights reserved.
*/
using Haply.Inverse.DeviceControllers;
using Haply.Samples.Tutorials.Utils;
using UnityEngine;
namespace Haply.Samples.Tutorials._5_SimplePositionControl
{
/// <summary>
/// Controls the Inverse3 cursor position based on the current position of this GameObject.
/// When the GameObject is within a specified distance from the cursor, it initiates synchronized control,
/// allowing the cursor to follow the GameObject's movements.
/// </summary>
[RequireComponent(typeof(MovableObject))]
public class SpherePositionControl : MonoBehaviour
{
public Inverse3Controller inverse3;
[Tooltip("Minimum distance required to initiate synchronized control between this GameObject and the Inverse3 cursor.")]
[Range(0, 1)]
public float minSyncDistance = 0.05f;
private bool _isCursorSynchronized;
private void Awake()
{
// Ensure inverse3 is set, finding it in the scene if necessary.
inverse3 ??= FindFirstObjectByType<Inverse3Controller>();
// When inverse3 is ready, so the handedness is defined
inverse3.Ready.AddListener((device, args) =>
{
// Teleport the sphere to its workspace center to ensure it can be reached,
// regardless of whether the device is left or right-handed. This ensures the GameObject starts in a
// position that is accessible by the Inverse3 device.
GetComponent<MovableObject>().SetTargetPosition(device.WorkspaceCenterLocalPosition, teleport:true);
});
}
private void OnDisable()
{
// Ensure movement synchronization is disabled when the component is disabled.
StopSynchronizeCursor();
inverse3.Release();
}
private void Update()
{
if (!inverse3.IsReady)
{
return;
}
// Calculate the distance between the Inverse3 position and this object's position.
var distance = Vector3.Distance(inverse3.CursorPosition, transform.position);
// Enable synchronized movement if within the minimum sync distance and not already synced.
if (!_isCursorSynchronized && distance <= minSyncDistance)
{
StartSynchronizeCursor();
}
// Disable synchronized movement if outside the minimum sync distance and currently synced.
else if (_isCursorSynchronized && distance > minSyncDistance)
{
StopSynchronizeCursor();
}
}
private void FixedUpdate()
{
if (_isCursorSynchronized)
{
// If in sync, set the Inverse3 cursor position to this object's position.
inverse3.SetCursorPosition(transform.position);
}
else
{
inverse3.ProbeCursorPosition(true);
}
}
private void StartSynchronizeCursor()
{
// Get the current cursor position.
var cursorPosition = inverse3.Cursor.transform.position;
// Teleport this object to the cursor position to avoid a sudden jump when position control starts.
GetComponent<MovableObject>().SetTargetPosition(cursorPosition, teleport:true);
// Start synchronizing the movement of this object with the cursor.
_isCursorSynchronized = true;
}
private void StopSynchronizeCursor()
{
// Stop synchronizing the movement.
_isCursorSynchronized = !inverse3.Release();
}
}
}