1
0
mirror of synced 2024-12-12 14:31:04 +01:00
WACVR/Assets/Script/Controller/HandFollowManager.cs

95 lines
3.2 KiB
C#
Raw Normal View History

2022-11-05 01:32:17 +01:00
using TMPro;
2022-10-16 15:42:49 +02:00
using UnityEngine;
2022-11-05 01:32:17 +01:00
using UnityEngine.UI;
2022-10-16 15:42:49 +02:00
public class HandFollowManager : MonoBehaviour
{
public GameObject Target;
public Transform Center;
2022-11-05 01:32:17 +01:00
public CEnum.handStabilization Mode;
2022-10-16 15:42:49 +02:00
public float VelocityThreshold = 0.1f;
2022-11-05 23:53:06 +01:00
private Rigidbody currentRigidbody;
2022-10-16 15:42:49 +02:00
private Rigidbody TargetRigidbody;
private Vector3 previousPosition;
2022-11-05 23:53:06 +01:00
2022-10-16 15:42:49 +02:00
private void Start()
{
TargetRigidbody = Target.GetComponent<Rigidbody>();
2022-11-05 23:53:06 +01:00
currentRigidbody = GetComponent<Rigidbody>();
2022-11-05 01:32:17 +01:00
2022-11-05 23:53:06 +01:00
var modeWidget = ConfigManager.GetConfigPanelWidget("HandTrackingMode");
2022-11-05 01:32:17 +01:00
var threshWidget = ConfigManager.GetConfigPanelWidget("Threshold");
var modeDropdown = modeWidget.GetComponent<TMP_Dropdown>();
var threshSlider = threshWidget.GetComponent<Slider>();
modeDropdown.onValueChanged.AddListener((int value) => {
VelocityThreshold = ConfigManager.config.Threshold;
2022-11-05 01:32:17 +01:00
Mode = (CEnum.handStabilization)value;
2022-11-05 23:53:06 +01:00
switch (Mode)
{
case CEnum.handStabilization.None:
currentRigidbody.isKinematic = true;
break;
case CEnum.handStabilization.Physics:
currentRigidbody.isKinematic = false;
break;
case CEnum.handStabilization.Velocity:
currentRigidbody.isKinematic = true;
break;
}
2022-11-05 01:32:17 +01:00
});
threshSlider.onValueChanged.AddListener((float value) => {
VelocityThreshold = value;
});
modeDropdown.onValueChanged?.Invoke(modeDropdown.value);
threshSlider.onValueChanged?.Invoke(threshSlider.value);
2022-10-16 15:42:49 +02:00
}
private void VelocityTracking()
{
Vector3 velocity = (Target.transform.position - previousPosition) / Time.deltaTime;
if (velocity.magnitude > VelocityThreshold)
{
transform.position = Target.transform.position;
2022-11-05 23:53:06 +01:00
//PhysicsMove(Target.transform);
2022-10-16 15:42:49 +02:00
}
previousPosition = Target.transform.position;
}
2022-11-05 23:53:06 +01:00
private void PhysicsMove(Transform targetTransform)
{
currentRigidbody.velocity = (targetTransform.position - transform.position) / Time.fixedDeltaTime;
Quaternion rotationDelta = targetTransform.rotation * Quaternion.Inverse(transform.rotation);
rotationDelta.ToAngleAxis(out float angle, out Vector3 axis);
Vector3 rotationDeltaInDegrees = angle * axis;
currentRigidbody.angularVelocity = rotationDeltaInDegrees * Mathf.Deg2Rad / Time.fixedDeltaTime;
}
2022-10-16 15:42:49 +02:00
private void Update()
{
gameObject.transform.localScale = Target.transform.localScale;
}
private void FixedUpdate()
{
switch (Mode)
{
2022-11-05 01:32:17 +01:00
case CEnum.handStabilization.None:
2022-10-16 15:42:49 +02:00
transform.position = Target.transform.position;
break;
2022-11-05 23:53:06 +01:00
case CEnum.handStabilization.Physics:
//transform.position = Target.transform.position;
PhysicsMove(Target.transform);
break;
case CEnum.handStabilization.Velocity:
VelocityTracking();
break;
2022-10-16 15:42:49 +02:00
}
}
}