構えの処理を追加
This commit is contained in:
58
Assets/Scripts/VRInputPoseDetector.cs
Normal file
58
Assets/Scripts/VRInputPoseDetector.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class VRInputPoseDetector : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Transform headTransform;
|
||||
[SerializeField] private Transform rightHandTransform;
|
||||
[SerializeField] private Transform leftHandTransform;
|
||||
[SerializeField] private float minForwardDistance = 0.15f;
|
||||
[SerializeField] private float maxForwardDistance = 0.5f;
|
||||
[SerializeField] private float minHeightOffset = -0.35f;
|
||||
[SerializeField] private float maxHeightOffset = -0.1f;
|
||||
[SerializeField] private float requiredPoseTime = -0.3f;
|
||||
private float poseTimer = 0f;
|
||||
private bool isPoseMainTained = false;
|
||||
public bool IsPosing => isPoseMainTained;
|
||||
|
||||
void Update()
|
||||
{
|
||||
if(headTransform == null || rightHandTransform == null || leftHandTransform == null) return;
|
||||
|
||||
//両手の世界座標を頭を中心としたローカル座標に変換
|
||||
Vector3 localRightHand = headTransform.InverseTransformPoint(rightHandTransform.position);
|
||||
Vector3 localLeftHand = headTransform.InverseTransformPoint(leftHandTransform.position);
|
||||
|
||||
//両手が構え範囲に入っているかチェック
|
||||
bool isRightHandGuarding = CheckHandPosition(localRightHand);
|
||||
bool isLeftHandGuarding = CheckHandPosition(localLeftHand);
|
||||
|
||||
//両手が構え範囲にある場合のタイマー処理
|
||||
if(isRightHandGuarding && isLeftHandGuarding)
|
||||
{
|
||||
poseTimer += Time.deltaTime;
|
||||
if(poseTimer >= requiredPoseTime && !isPoseMainTained)
|
||||
{
|
||||
isPoseMainTained = true;
|
||||
Debug.Log("kamaeseikou");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
poseTimer = 0f;
|
||||
if(isPoseMainTained)
|
||||
{
|
||||
isPoseMainTained = false;
|
||||
Debug.Log("kamaesippai");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
private bool CheckHandPosition(Vector3 localHandPos)
|
||||
{
|
||||
bool inForwardRange = localHandPos.z >= minForwardDistance && localHandPos.z <= maxForwardDistance; //Z軸:頭より前に手があるか
|
||||
bool inHeightRange = localHandPos.y >= minHeightOffset && localHandPos.y <= maxHeightOffset; //Y軸:手の高さが適切か
|
||||
|
||||
return inForwardRange && inHeightRange;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user