132 lines
4.2 KiB
C#
132 lines
4.2 KiB
C#
using System.Collections.Generic;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
|
|
public class Draws : MonoBehaviour
|
|
{
|
|
private LineRenderer lineRenderer;
|
|
// private BoxCollider boxCollider;
|
|
public Transform parent;
|
|
public List<Vector3> localOffsets = new List<Vector3>();
|
|
|
|
private Vector3 initialScale; // ذخیره مقیاس اولیه
|
|
|
|
public void Start()
|
|
{
|
|
lineRenderer = GetComponent<LineRenderer>();
|
|
// boxCollider = parent.gameObject.AddComponent<BoxCollider>(); // اضافه کردن BoxCollider
|
|
initialScale = transform.lossyScale; // ذخیره مقیاس اولیه
|
|
localOffsets = GetLinePositionsList(); // ذخیره موقعیتهای اولیه نقاط
|
|
UpdateCollider();
|
|
}
|
|
|
|
public bool setIn;
|
|
|
|
public void Update()
|
|
{
|
|
if (localOffsets.Count >= lineRenderer.positionCount)
|
|
{
|
|
if (!setIn)
|
|
SetOwnerPositionInMiddle();
|
|
else
|
|
{
|
|
Vector3 currentScale = transform.lossyScale;
|
|
Vector3 scaleRatio = new Vector3(
|
|
currentScale.x / initialScale.x,
|
|
currentScale.y / initialScale.y,
|
|
currentScale.z / initialScale.z
|
|
);
|
|
|
|
for (int i = 0; i < localOffsets.Count; i++)
|
|
{
|
|
// اعمال نسبت تغییر مقیاس بر روی نقاط
|
|
Vector3 scaledOffset = Vector3.Scale(localOffsets[i], scaleRatio);
|
|
Vector3 rotatedOffset = transform.rotation * scaledOffset;
|
|
Vector3 newPosition = transform.position + rotatedOffset;
|
|
lineRenderer.SetPosition(i, newPosition);
|
|
}
|
|
}
|
|
|
|
// UpdateCollider(); // بهروزرسانی برخورد بعد از تغییر موقعیت نقاط
|
|
}
|
|
else
|
|
{
|
|
localOffsets = GetLinePositionsList();
|
|
}
|
|
}
|
|
|
|
public List<Vector3> GetLinePositionsList()
|
|
{
|
|
List<Vector3> positions = new List<Vector3>();
|
|
int pointCount = lineRenderer.positionCount;
|
|
|
|
for (int i = 0; i < pointCount; i++)
|
|
{
|
|
Vector3 pointPosition = lineRenderer.GetPosition(i);
|
|
Vector3 localOffset = Quaternion.Inverse(transform.rotation) * (pointPosition - transform.position);
|
|
positions.Add(localOffset);
|
|
}
|
|
|
|
return positions;
|
|
}
|
|
|
|
void SetOwnerPositionInMiddle()
|
|
{
|
|
setIn = true;
|
|
List<Vector3> list = GetLinePositionsList();
|
|
if (list.Count > 0)
|
|
{
|
|
Vector3 sum = Vector3.zero;
|
|
foreach (var offset in localOffsets)
|
|
{
|
|
sum += offset;
|
|
}
|
|
Vector3 middlePoint = sum / list.Count;
|
|
transform.position = middlePoint;
|
|
}
|
|
parent.transform.position = transform.position;
|
|
transform.parent = parent;
|
|
localOffsets = GetLinePositionsList();
|
|
initialScale = transform.lossyScale; // ذخیره مجدد مقیاس اولیه پس از تنظیم مرکز
|
|
UpdateCollider();
|
|
}
|
|
|
|
public Bounds GetBoundingBox()
|
|
{
|
|
if (lineRenderer.positionCount == 0)
|
|
return new Bounds(Vector3.zero, Vector3.zero);
|
|
|
|
Vector3 min = lineRenderer.GetPosition(0);
|
|
Vector3 max = min;
|
|
|
|
for (int i = 1; i < lineRenderer.positionCount; i++)
|
|
{
|
|
Vector3 point = lineRenderer.GetPosition(i);
|
|
min = Vector3.Min(min, point);
|
|
max = Vector3.Max(max, point);
|
|
}
|
|
|
|
Vector3 size = max - min;
|
|
Vector3 center = (max + min) / 2;
|
|
|
|
return new Bounds(center, size);
|
|
}
|
|
|
|
public void UpdateCollider()
|
|
{
|
|
//if (boxCollider == null)
|
|
// return;
|
|
|
|
Bounds bounds = GetBoundingBox();
|
|
//boxCollider.center = transform.InverseTransformPoint(bounds.center); // تبدیل به مختصات محلی
|
|
// boxCollider.size = bounds.size;
|
|
parent.transform.localScale = bounds.size;
|
|
if (!first&&setIn)
|
|
{
|
|
parent.transform.AddComponent<BoxCollider>();
|
|
first = true;
|
|
}
|
|
}
|
|
bool first;
|
|
}
|