간단한 위치 제어 튜토리얼
이 튜토리얼에서는 시작 가이드에서 소개된 개념을 바탕으로, Unity 장면 내의 동적 게임 오브젝트를 이용해 Inverse3 장치의 커서 위치를 제어하는 방법을 보여줍니다.
소개
Inverse3 장치는 **힘 제어(Force Control)**와 **위치 제어(Position Control)**의 두 가지 제어 모드를 지원합니다.
전자는 커서의 위치에 따라 힘을 조절하는 반면, 후자는 커서의 위치를 직접 조작합니다.
이 튜토리얼은 동적 장면에서의 힘 피드백 튜토리얼의 장면 설정을 활용하여, 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 장치 컴포넌트에 대한 참조입니다.
- 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 모드를 활성화한 후 커서를 Moving Ball에 가까이 가져가면, Inverse3 장치가 Moving Ball의 움직임을 따라가게 됩니다. 키보드 입력을 사용하여 Moving Ball을 움직일 수 있으며, 이를 통해 커서가 위치를 추적하는 능력을 확인할 수 있습니다.

소스 파일
이 튜토리얼의 전체 장면과 관련 파일은 Unity 패키지 관리자의 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();
}
}
}