Files
aibis-dream/Assets/Scripts/FixSystem/Line/LineManager.cs
T
2024-08-21 22:13:57 +08:00

58 lines
2.3 KiB
C#

using UnityEngine;
public class LineManager : MonoBehaviour
{
//public GameObject interactiveLineSegmentPrefab;
public GameObject visualLineSegmentPrefab;
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
// Convert the mouse position to a 2D ray
Vector2 ray = Camera.main.ScreenToWorldPoint(Input.mousePosition);
// Check if the ray hits a line segment
RaycastHit2D hit = Physics2D.Raycast(ray, Vector2.zero);
if (hit.collider != null)
{
Debug.Log("Raycast hit: " + hit.transform.name);
InteractiveLineSegment originalLineSegment = hit.transform.GetComponent<InteractiveLineSegment>();
if (originalLineSegment != null)
{
Debug.Log("Line segment hit: " + originalLineSegment.name);
// Calculate the split point
Vector3 splitPoint = hit.point;
// Create the first new line segment
GameObject firstLineSegmentObject = Instantiate(visualLineSegmentPrefab, transform);
VisualLineSegment firstLineSegment = firstLineSegmentObject.GetComponent<VisualLineSegment>();
firstLineSegment.Initialize(originalLineSegment.StartPoint, splitPoint);
// Create the second new line segment
GameObject secondLineSegmentObject = Instantiate(visualLineSegmentPrefab, transform);
VisualLineSegment secondLineSegment = secondLineSegmentObject.GetComponent<VisualLineSegment>();
secondLineSegment.Initialize(originalLineSegment.EndPoint,splitPoint);
// Immediately destroy the original line segment
Destroy(originalLineSegment.gameObject);
// Shrink and disappear the new line segments
firstLineSegment.ShrinkAndDisappear();
secondLineSegment.ShrinkAndDisappear();
}
else
{
Debug.Log("Hit object is not a line segment");
}
}
else
{
Debug.Log("No raycast hit");
}
}
}
}