본문으로 이동

간단한 위치 제어 튜토리얼

이 튜토리얼에서는 ‘시작 가이드’에서 소개된 개념을 바탕으로, Unity 씬 내의 동적 게임 오브젝트를 사용하여 Inverse3 기기의 커서 위치를 제어하는 방법을 설명합니다.

소개​

Inverse3 기기는 두 가지 제어 모드를 지원합니다: 힘 제어 et 위치 제어. 전자는 커서 위치에 따라 힘을 조절하는 반면, 후자는 커서 위치를 직접 조작합니다. 이 튜토리얼은 동적 장면에서 힘의 피드백에 관한 튜토리얼, 다음의 사용에 중점을 둡니다. CursorSetPosition 메서드 내에서 FixedUpdate 기기의 커서가 움직이는 게임 내 물체를 따라가도록 하기 위해.

장면 설정​

다음에 설명된 장면 구성을 기준으로 시작하십시오. 동적 장면에서 힘의 피드백에 관한 튜토리얼, 여기에는 다음이 포함되어 있습니다. 햅틱 리그 그리고 게임 내 아이템 움직이는 공 구성 요소에 의해 제어됨 MovableObject.

SpherePositionControl 컴포넌트 구현​

부품을 교체하십시오 SphereForceFeedback 게임의 대상 움직이는 공 "라는 이름의 새로운 C# 스크립트를 통해 SpherePositionControl.cs. 클래스에서 다음 속성을 정의하십시오. 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();
}
}

Dans FixedUpdate, 커서의 위치를 다음의 위치와 일치하도록 업데이트하십시오. 움직이는 공 동기화가 활성화된 경우:

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

FixedUpdate 메서드를 사용하면 그래픽 렌더링 성능과 관계없이 위치 제어 기능이 안정적인 프레임 속도로 작동하도록 할 수 있습니다.

초기화​

Dans Awake(), 다음 사항을 확인하십시오. 움직이는 공 Inverse3 장치가 도달할 수 있는 위치에서 시작하며, 장치의 좌우 방향에 관계없이:

private void Awake()
{
inverse3.Ready.AddListener(device =>
{
GetComponent<MovableObject>().SetTargetPosition(((Inverse3)device).WorkspaceCenterLocalPosition, teleport: true);
});
}

게임 플레이​

Inverse3 를 고정하고, 기기에 충분한 공간이 확보되어 있는지 확인하십시오. 플레이 모드를 활성화하고 커서를 Moving Ball에 가까이 가져가면, Inverse3 기기가 Moving Ball의 움직임을 따라갑니다. 키보드 입력을 통해 Moving Ball을 이동시킬 수 있으며, 이를 통해 커서가 Moving Ball의 위치를 따라가는 기능을 확인할 수 있습니다.

간단한 위치 제어

소스 파일​

이 튜토리얼과 관련된 전체 장면 및 관련 파일은 예제에서 확인할 수 있습니다. 튜토리얼 Unity의 패키지 관리자. 여기에는 스크립트가 포함되어 있습니다. 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();
}
}
}