2025-04-21 20:45:50 +08:00
|
|
|
using DCFApixels.DebugXCore.Samples.Internal;
|
2025-02-23 10:48:25 +08:00
|
|
|
using UnityEngine;
|
|
|
|
|
2025-02-24 10:59:58 +08:00
|
|
|
namespace DCFApixels.DebugXCore.Samples
|
2025-02-23 10:48:25 +08:00
|
|
|
{
|
2025-02-23 20:03:13 +08:00
|
|
|
[SelectionBase]
|
2025-02-23 10:48:25 +08:00
|
|
|
public class DebugXSample_Raycasts2D : MonoBehaviour
|
|
|
|
{
|
|
|
|
public Gradient Gradient;
|
|
|
|
public float GradientMultiplier = 5;
|
|
|
|
public Transform[] Points;
|
2025-04-21 20:26:42 +08:00
|
|
|
public Transform WarrningPoint;
|
|
|
|
|
2025-07-23 19:05:25 +08:00
|
|
|
public Transform RotatedTransform;
|
|
|
|
public float RotationSpeed = 30;
|
2025-02-23 10:48:25 +08:00
|
|
|
|
2025-02-23 20:03:13 +08:00
|
|
|
#if UNITY_EDITOR
|
2025-02-23 10:48:25 +08:00
|
|
|
private void OnDrawGizmos()
|
2025-02-23 20:03:13 +08:00
|
|
|
{
|
|
|
|
Draw();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
private void Update()
|
|
|
|
{
|
|
|
|
Draw();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
private void Draw()
|
2025-02-23 10:48:25 +08:00
|
|
|
{
|
2025-03-12 21:49:38 +08:00
|
|
|
#if DEBUGX_ENABLE_PHYSICS2D
|
2025-02-23 10:48:25 +08:00
|
|
|
int i = 0;
|
|
|
|
const float RADIUS_M = 0.5f;
|
|
|
|
|
|
|
|
Transform point;
|
|
|
|
Ray ray;
|
|
|
|
RaycastHit2D hit;
|
|
|
|
|
|
|
|
point = Points[i++];
|
|
|
|
ray = new Ray(point.position, point.forward);
|
|
|
|
hit = Physics2D.Raycast(ray.origin, ray.direction, float.PositiveInfinity, -1);
|
|
|
|
DebugX.Draw(GetColor(point)).Raycast2D(ray, hit);
|
|
|
|
|
|
|
|
point = Points[i++];
|
|
|
|
ray = new Ray(point.position, point.forward);
|
|
|
|
hit = Physics2D.CircleCast(ray.origin, point.localScale.x * RADIUS_M, ray.direction, float.PositiveInfinity, int.MaxValue);
|
|
|
|
DebugX.Draw(GetColor(point)).CircleCast2D(ray, point.localScale.x * RADIUS_M, hit);
|
|
|
|
|
|
|
|
point = Points[i++];
|
|
|
|
ray = new Ray(point.position, point.forward);
|
|
|
|
hit = Physics2D.BoxCast(ray.origin, point.localScale, point.eulerAngles.z, ray.direction, float.PositiveInfinity, int.MaxValue);
|
|
|
|
DebugX.Draw(GetColor(point)).BoxCast2D(ray, point.eulerAngles.z, point.localScale, hit);
|
|
|
|
|
|
|
|
point = Points[i++];
|
|
|
|
ray = new Ray(point.position, point.forward);
|
|
|
|
hit = Physics2D.CapsuleCast(ray.origin, point.localScale, CapsuleDirection2D.Vertical, point.eulerAngles.z, ray.direction, float.PositiveInfinity, int.MaxValue);
|
|
|
|
DebugX.Draw(GetColor(point)).CapsuleCast2D(ray, point.eulerAngles.z, point.localScale, CapsuleDirection2D.Vertical, hit);
|
2025-04-21 20:26:42 +08:00
|
|
|
#else
|
2025-04-21 20:45:50 +08:00
|
|
|
DebugX.Draw(GetColor(WarrningPoint).Inverse()).Text(WarrningPoint.position, "Add \"DEBUGX_ENABLE_PHYSICS2D\" define", DebugXTextSettings.WorldSpaceScale.SetSize(22).SetAnchor(TextAnchor.MiddleCenter));
|
2025-03-12 21:49:38 +08:00
|
|
|
#endif
|
2025-07-23 19:05:25 +08:00
|
|
|
|
|
|
|
if (Application.isPlaying && RotatedTransform)
|
|
|
|
{
|
|
|
|
RotatedTransform.Rotate(new Vector3(0, RotationSpeed * Time.deltaTime, 0));
|
|
|
|
}
|
2025-02-23 10:48:25 +08:00
|
|
|
}
|
|
|
|
private Color GetColor(Transform pos1)
|
|
|
|
{
|
2025-04-21 20:45:50 +08:00
|
|
|
return Gradient.Evaluate(pos1, GradientMultiplier);
|
2025-04-21 20:26:42 +08:00
|
|
|
}
|
2025-02-23 10:48:25 +08:00
|
|
|
}
|
|
|
|
}
|