using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// 电线管理
///
public class WireCreater : MonoBehaviour
{
public static WireCreater Instance;
///
/// 实时连线(消耗性能)
///
public bool realTimeWire = false;
///
/// 弧垂
///
public float wireRatio = 0.03f;
///
/// 线宽
///
public float wireWidth = 0.2f;
///
/// 线色
///
public Color wireColor;
///
/// 碰撞数量
///
public int positionCount = 13;
public Material linmaterial;
void Awake()
{
Instance = this;
}
///
/// 创建电线模型
///
/// 父物体
/// 名称
///
///
/// 弧垂比例
private void CreateNewLineObj(GameObject fatherGo, Vector3 start, Vector3 end, float SagRatio = 0.03f)
{
GameObject newLine = new GameObject("line");
LineRenderer lr = newLine.AddComponent();
lr.startWidth = wireWidth;
lr.endWidth = wireWidth;
lr.positionCount = positionCount;
//Material mat = new Material(linmaterial);
//mat.color = new Color32(56, 56, 56, 255);
lr.material = linmaterial;
for (int i = 0; i < lr.positionCount; i++)
{
lr.SetPosition(i, SagHeight(i, start, end, SagRatio, lr.positionCount - 1));
}
CreateColliderLine(lr, "ChildCom");
}
///
/// 电线弧度的计算方式
///
///
///
///
///
///
///
Vector3 SagHeight(int index, Vector3 start, Vector3 end, float SagRatio, int allNo)
{
float index2 = (float)index;
float l = Vector3.Distance(start, end);//2电线杆之间的距离
float h = Vector3.Distance(start, end) * SagRatio;//最大弧垂距离
float a = (4 * (-h) / (l * l));//ax2+bx+c
float b = (4 * h) / l;
float f = l / allNo;//这个是分隔的每一段距离
float HightDis = a * index * index * f * f + b * index * f;
Vector3 h1 = start * (1 - index2 / allNo) + end * (index2 / allNo);//这个是不加弧垂时候的点
h1 = h1 - new Vector3(0, HightDis, 0);
return h1;
}
///
/// 创建电线模型
///
/// 父物体
/// 名称
///
///
/// 弧垂比例
public void CreateNewLine(WireObject dw, Vector3 start, Vector3 end, float SagRatio = 0.03f)
{
LineRenderer lr = dw.gameObject.GetComponent();
lr.startWidth = wireWidth;
lr.endWidth = wireWidth;
lr.positionCount = positionCount;
for (int i = 0; i < lr.positionCount; i++)
{
lr.SetPosition(i, SagHeight(i, start, end, SagRatio, lr.positionCount - 1));
}
CreateColliderLine(lr, "ChildCom");
}
///
/// 给lineRender
///
///
public static void CreateColliderLine(LineRenderer lr, string ChildName)
{
return;
GameObject line = lr.gameObject;
List point = new List();
for (int i = 0; i < lr.positionCount; i++)
{
point.Add(lr.GetPosition(i));
}
for (int i = 0; i < point.Count - 1; i++)
{
GameObject newgo = new GameObject(ChildName);
//newgo = line;
newgo.transform.SetParent(line.transform);
CapsuleCollider capsule = newgo.AddComponent();
capsule.direction = 2;
capsule.radius = lr.startWidth * 2.5f;
capsule.center = Vector3.zero;
capsule.transform.position = point[i] + (point[i + 1] - point[i]) / 2;
capsule.transform.LookAt(point[i]);
capsule.height = (point[i + 1] - point[i]).magnitude;
}
}
}