100 lines
2.6 KiB
C#
100 lines
2.6 KiB
C#
using UnityEngine;
|
|
using UnityEngine.XR;
|
|
|
|
public class FishingHapticManager : MonoBehaviour
|
|
{
|
|
[Header("Haptic Target")]
|
|
[SerializeField] private XRNode targetHand = XRNode.RightHand;
|
|
|
|
[Header("Haptic Settings")]
|
|
[SerializeField] private bool useHaptic = true;
|
|
[SerializeField] private bool showHapticDebugLog = false;
|
|
|
|
[SerializeField] private float perfectAmplitude = 1f;
|
|
[SerializeField] private float perfectDuration = 0.2f;
|
|
|
|
[SerializeField] private float goodAmplitude = 0.5f;
|
|
[SerializeField] private float goodDuration = 0.1f;
|
|
|
|
[SerializeField] private float missAmplitude = 0.2f;
|
|
[SerializeField] private float missDuration = 0.05f;
|
|
|
|
public XRNode TargetHand => targetHand;
|
|
|
|
private void OnValidate()
|
|
{
|
|
perfectAmplitude = Mathf.Clamp01(perfectAmplitude);
|
|
goodAmplitude = Mathf.Clamp01(goodAmplitude);
|
|
missAmplitude = Mathf.Clamp01(missAmplitude);
|
|
|
|
perfectDuration = Mathf.Max(0f, perfectDuration);
|
|
goodDuration = Mathf.Max(0f, goodDuration);
|
|
missDuration = Mathf.Max(0f, missDuration);
|
|
}
|
|
|
|
private void SendHaptic(float amplitude, float duration)
|
|
{
|
|
if (!useHaptic)
|
|
return;
|
|
|
|
InputDevice device = InputDevices.GetDeviceAtXRNode(targetHand);
|
|
|
|
if (!device.isValid)
|
|
{
|
|
LogHapticWarning("Haptic device not found.");
|
|
return;
|
|
}
|
|
|
|
if (!device.TryGetHapticCapabilities(out HapticCapabilities capabilities))
|
|
{
|
|
LogHapticWarning("Haptic capabilities not available.");
|
|
return;
|
|
}
|
|
|
|
if (!capabilities.supportsImpulse)
|
|
{
|
|
LogHapticWarning("Haptic impulse is not supported on this device.");
|
|
return;
|
|
}
|
|
|
|
if (capabilities.numChannels < 1)
|
|
{
|
|
LogHapticWarning("Haptic channel count is zero.");
|
|
return;
|
|
}
|
|
|
|
device.SendHapticImpulse(0u, Mathf.Clamp01(amplitude), Mathf.Max(0f, duration));
|
|
}
|
|
|
|
private void LogHapticWarning(string message)
|
|
{
|
|
if (showHapticDebugLog)
|
|
Debug.LogWarning($"[FishingHapticManager] {message}", this);
|
|
}
|
|
|
|
public void SetTargetHand(XRNode hand)
|
|
{
|
|
targetHand = hand;
|
|
}
|
|
|
|
public void SetUseHaptic(bool value)
|
|
{
|
|
useHaptic = value;
|
|
}
|
|
|
|
public void Perfect()
|
|
{
|
|
SendHaptic(perfectAmplitude, perfectDuration);
|
|
}
|
|
|
|
public void Good()
|
|
{
|
|
SendHaptic(goodAmplitude, goodDuration);
|
|
}
|
|
|
|
public void Miss()
|
|
{
|
|
SendHaptic(missAmplitude, missDuration);
|
|
}
|
|
}
|