using UnityEngine; using UnityEngine.InputSystem; // Input Systemを使うために必要 using UnityEngine.XR; // 振動機能のために必要 [RequireComponent(typeof(Collider))] [RequireComponent(typeof(Rigidbody))] public class VRInputPunch : MonoBehaviour { [SerializeField] private InputActionProperty velocityAction; [SerializeField] private XRNode controllerNode = XRNode.RightHand; [SerializeField] private VRInputPoseDetector poseDetector; [SerializeField] private float minPunchSpeed = 2.0f; //パンチとして判定する最低速度 (m/s) [SerializeField] private float damageMultiplier = 10.0f; //ダメージ倍率 [SerializeField] private float hapticAmplitude = 0.5f; //振動の強さ [SerializeField] private float hapticDuration = 0.1f; //振動の長さ private void OnEnable() { if (velocityAction.action != null) { velocityAction.action.Enable(); } } private void OnDisable() { if (velocityAction.action != null) { velocityAction.action.Disable(); } } void Start() { Rigidbody rb = GetComponent(); rb.isKinematic = true; rb.useGravity = false; GetComponent().isTrigger = true; } private void OnTriggerEnter(Collider other) { Vector3 velocity = velocityAction.action.ReadValue(); float speed = velocity.magnitude; Debug.Log($"[InputSystem接触] {other.name} に触れました。センサー速度: {speed:F2} m/s"); if (speed >= minPunchSpeed) { IDamageble damageble = other.GetComponentInParent(); if (damageble != null) { DamageInfo info = new DamageInfo(); if(poseDetector.IsPosing) { info.amount = (speed * damageMultiplier) * 2f; info.isPoseBonus = true; } else { info.amount = speed * damageMultiplier; info.isPoseBonus = false; } info.hitPosition = other.transform.position; info.punchDirection = velocity.normalized; //パンチの進む向き damageble.TakeDamage(info); Debug.Log("パンチ成功 ダメージ: {damage:F1}"); TriggerHaptics(); } else { Debug.Log("スピードが足りません。必要: {minPunchSpeed} / 現在: {speed:F2}"); } } } // コントローラーを振動させる処理 private void TriggerHaptics() { UnityEngine.XR.InputDevice device = InputDevices.GetDeviceAtXRNode(controllerNode); if (device.isValid) { device.SendHapticImpulse(0, hapticAmplitude, hapticDuration); } } }