2024-08-03 22:24:42 +02:00
|
|
|
|
using UnityEngine;
|
2023-01-22 16:38:23 +01:00
|
|
|
|
|
|
|
|
|
namespace ABI.CCK.Components
|
|
|
|
|
{
|
|
|
|
|
[ExecuteInEditMode]
|
2024-08-03 22:24:42 +02:00
|
|
|
|
[AddComponentMenu("ChilloutVR/CVR Distance Constraint")]
|
|
|
|
|
[HelpURL("https://developers.abinteractive.net/cck/")]
|
|
|
|
|
public class CVRDistanceConstrain : MonoBehaviour, ICCK_Component
|
2023-01-22 16:38:23 +01:00
|
|
|
|
{
|
|
|
|
|
public Transform target;
|
|
|
|
|
public float minDistance = 0;
|
|
|
|
|
public float maxDistance = 0;
|
|
|
|
|
|
|
|
|
|
[ReadOnly]
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private float currentDistance = 0f;
|
|
|
|
|
|
|
|
|
|
public void OnDrawGizmosSelected()
|
|
|
|
|
{
|
|
|
|
|
if (target == null) return;
|
|
|
|
|
|
|
|
|
|
if (maxDistance < minDistance && maxDistance != 0f) return;
|
|
|
|
|
|
|
|
|
|
Vector3 direction = (transform.position - target.position).normalized;
|
|
|
|
|
|
|
|
|
|
if (minDistance == 0)
|
|
|
|
|
{
|
|
|
|
|
if (maxDistance == 0)
|
|
|
|
|
{
|
|
|
|
|
Gizmos.color = Color.green;
|
|
|
|
|
Gizmos.DrawRay(target.position, direction * 9999f);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Gizmos.color = Color.green;
|
|
|
|
|
Gizmos.DrawLine(target.position, target.position + direction * maxDistance);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (maxDistance == 0)
|
|
|
|
|
{
|
|
|
|
|
Gizmos.color = Color.red;
|
|
|
|
|
Gizmos.DrawLine(target.position, target.position + direction * minDistance);
|
|
|
|
|
Gizmos.color = Color.green;
|
|
|
|
|
Gizmos.DrawRay(target.position + direction * minDistance, direction * 9999f);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Gizmos.color = Color.red;
|
|
|
|
|
Gizmos.DrawLine(target.position, target.position + direction * minDistance);
|
|
|
|
|
Gizmos.color = Color.green;
|
|
|
|
|
Gizmos.DrawLine(target.position + direction * minDistance, target.position + direction * maxDistance);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update()
|
|
|
|
|
{
|
|
|
|
|
if (target == null)
|
|
|
|
|
{
|
|
|
|
|
currentDistance = 0f;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
currentDistance = Vector3.Distance(transform.position, target.position);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|