66 lines
1.8 KiB
C#
66 lines
1.8 KiB
C#
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
|
|
public class InteractiveLineSegment : VisualLineSegment
|
|
{
|
|
[SerializeField]
|
|
private Transform startTransform;
|
|
public Transform StartTransform
|
|
{
|
|
get { return startTransform; }
|
|
set { startTransform = value; }
|
|
}
|
|
|
|
[SerializeField]
|
|
private Transform endTransform;
|
|
public Transform EndTransform
|
|
{
|
|
get { return endTransform; }
|
|
set { endTransform = value; }
|
|
}
|
|
|
|
private BoxCollider2D boxCollider;
|
|
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
boxCollider = GetComponent<BoxCollider2D>();
|
|
}
|
|
public void Start()
|
|
{
|
|
Initialize();
|
|
|
|
}
|
|
|
|
|
|
public void Initialize()
|
|
{
|
|
if (StartTransform == null || EndTransform == null)
|
|
{
|
|
Debug.LogError("StartTransform or EndTransform is not set.");
|
|
return;
|
|
}
|
|
|
|
base.Initialize(StartTransform.position, EndTransform.position);
|
|
|
|
// Calculate the length of the line segment
|
|
float length = Vector3.Distance(StartPoint, EndPoint);
|
|
|
|
// Calculate the direction of the line segment
|
|
Vector3 direction = (EndPoint - StartPoint).normalized;
|
|
|
|
// Set the size of the BoxCollider2D
|
|
boxCollider.size = new Vector2(length, 0.6f); // Change 0.1f to the desired thickness of the line
|
|
|
|
// Set the center of the BoxCollider2D
|
|
boxCollider.offset = new Vector2(length / 2, 0);
|
|
|
|
// Rotate the BoxCollider2D to align with the line segment
|
|
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
|
|
boxCollider.transform.rotation = Quaternion.Euler(0, 0, angle);
|
|
|
|
// Set the position of the BoxCollider2D to the start point
|
|
boxCollider.transform.position = StartPoint;
|
|
}
|
|
}
|