92 lines
2.5 KiB
C#
92 lines
2.5 KiB
C#
|
|
using UdonSharp;
|
|
using UnityEngine;
|
|
using VRC.SDKBase;
|
|
using VRC.Udon;
|
|
|
|
|
|
[UdonBehaviourSyncMode(BehaviourSyncMode.Continuous)]
|
|
public class PlayerHorseshoe : UdonSharpBehaviour
|
|
{
|
|
[SerializeField] private Transform[] _ValidStakes;
|
|
[Space]
|
|
[SerializeField] private Rigidbody _HorseshoeRigidBody;
|
|
[SerializeField] private PlayerHorseshoeStakeDetection _RingerDetector;
|
|
[SerializeField] private PlayerHorseshoeStakeDetection _LeanDetector;
|
|
[SerializeField] private Transform[] _DistanceDetectors;
|
|
|
|
private bool _CheckMotion = false;
|
|
private float _CheckMotionTimer = 0.0f;
|
|
|
|
|
|
void FixedUpdate()
|
|
{
|
|
if (_CheckMotion)
|
|
{
|
|
if (_HorseshoeRigidBody.velocity.magnitude < 0.0000000000001f &&
|
|
_HorseshoeRigidBody.angularVelocity.sqrMagnitude < 0.0000000000001f)
|
|
{
|
|
_CheckMotionTimer += Time.fixedDeltaTime;
|
|
if (_CheckMotionTimer >= 0.5f)
|
|
{
|
|
_CheckMotion = false;
|
|
_CheckMotionTimer = 0.0f;
|
|
|
|
if (_RingerDetector.StakeCollision)
|
|
{
|
|
Debug.Log("[PlayerHorseshoe] Ringer!!!");
|
|
}
|
|
//else if (_LeanDetector.StakeCollision)
|
|
//{
|
|
// Debug.LogError("[PlayerHorseshoe] Leaning on stake.");
|
|
//}
|
|
else
|
|
{
|
|
// First, figure out which stake we're closest to
|
|
Vector3 ClosestStakePosition = Vector3.zero;
|
|
float ClosestStakeDistance = float.MaxValue;
|
|
foreach (Transform StakeTransform in _ValidStakes)
|
|
{
|
|
float DistanceToStake = Vector3.Distance(transform.position, StakeTransform.position);
|
|
if (DistanceToStake < ClosestStakeDistance)
|
|
{
|
|
ClosestStakeDistance = DistanceToStake;
|
|
ClosestStakePosition = StakeTransform.position;
|
|
}
|
|
}
|
|
|
|
// Then determine which distance detector is closest to this stake
|
|
float ClosestShoeDistance = float.MaxValue;
|
|
foreach (Transform DistanceDetector in _DistanceDetectors)
|
|
{
|
|
float DistanceToStake = Vector3.Distance(DistanceDetector.position, ClosestStakePosition);
|
|
if (DistanceToStake < ClosestShoeDistance)
|
|
{
|
|
ClosestShoeDistance = DistanceToStake;
|
|
}
|
|
}
|
|
|
|
// At this point, we know the shortest distance between the shoe and stake
|
|
Debug.Log("[PlayerHorseshoe] Detected distance: " + ClosestShoeDistance);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_CheckMotionTimer = 0.0f;
|
|
}
|
|
}
|
|
}
|
|
|
|
public override void OnDrop()
|
|
{
|
|
SendCustomEventDelayedSeconds(nameof(StartCheckingMotion), 0.05f);
|
|
base.OnDrop();
|
|
}
|
|
public void StartCheckingMotion()
|
|
{
|
|
_CheckMotion = true;
|
|
_CheckMotionTimer = 0.0f;
|
|
}
|
|
}
|