メインコンテンツへスキップ

簡単な位置制御のチュートリアル

このチュートリアルでは、Unityのシーン内で、動的なゲームオブジェクトを使用してInverse3 デバイスのカーソル位置を制御する方法を解説します。内容は、「入門ガイド」で紹介されている概念に基づいています。

概要​

Inverse3 本機器は2種類の制御モードに対応しています:力制御和位置制御。 前者はカーソルの位置に応じて力の強さを調整し、後者はカーソルの位置を直接操作します。 このチュートリアルでは、 動的シーンにおけるフォースフィードバックのチュートリアル シーンの設定においては、特に FixedUpdate 方法での使用 CursorSetPosition,これにより、デバイスのカーソルが移動するゲームオブジェクトに追従できるようになります。

シーン設定​

まずは以下の手順に従って 動的シーンにおけるフォースフィードバックのチュートリアル 中に記載されているシーン設定を行い、その中には1つの ハプティック・リグ および MovableObject コンポーネント制御の ムービング・ボール ゲームオブジェクト。

SpherePositionControl コンポーネントを実装する​

将 ムービング・ボール ゲームオブジェクト上の スフィアフォースフィードバック コンポーネントを、 SpherePositionControl.cs 新しい C# スクリプト。 で SpherePositionControl このクラスには、以下のプロパティが定義されています:

public Inverse3 inverse3;
public float minSyncDistance = 0.05f;
private bool _isCursorSynchronized;
  • inverse3:Inverse3 のデバイスコンポーネントへの参照は、インスペクタで設定します。
  • 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),それは ムービングボール カーソルの位置へ瞬時に移動する。

Update および FixedUpdate メソッド​

で Update この方法では、カーソルと ムービング・ボール 間の距離によるカーソルの同期状態の切り替え:

private void Update()
{
var distance = Vector3.Distance(inverse3.CursorPosition, transform.position);
if (!_isCursorSynchronized && distance <= minSyncDistance)
{
StartSynchronizeCursor();
}
else if (_isCursorSynchronized && distance > minSyncDistance)
{
StopSynchronizeCursor();
}
}

で FixedUpdate その中で、同期が有効になっている場合は、カーソルの位置を ムービング・ボール 一致を維持:

private void FixedUpdate()
{
if (_isCursorSynchronized)
{
inverse3.SetCursorPosition(transform.position);
}
}

FixedUpdate メソッドを使用することで、グラフィックのレンダリング性能に影響されることなく、位置制御を安定したフレームレートで実行できるようになります。

初期化​

で Awake() その際、左右の手の位置にかかわらず、必ず ムービング・ボール その開始位置は、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 の チュートリアル 例の中にあります。 そこには以下が含まれています。 MovableObject スクリプト。このスクリプトは、キーボード入力によってゲームオブジェクトの移動を制御するために、いくつかの例で使用されています。

スフィアポジションコントロール.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();
}
}
}