138 lines
4.4 KiB
C#
138 lines
4.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// 电线管理
|
|
/// </summary>
|
|
public class WireCreater : MonoBehaviour
|
|
{
|
|
public static WireCreater Instance;
|
|
/// <summary>
|
|
/// 实时连线(消耗性能)
|
|
/// </summary>
|
|
public bool realTimeWire = false;
|
|
/// <summary>
|
|
/// 弧垂
|
|
/// </summary>
|
|
public float wireRatio = 0.03f;
|
|
/// <summary>
|
|
/// 线宽
|
|
/// </summary>
|
|
public float wireWidth = 0.2f;
|
|
/// <summary>
|
|
/// 线色
|
|
/// </summary>
|
|
public Color wireColor;
|
|
/// <summary>
|
|
/// 碰撞数量
|
|
/// </summary>
|
|
public int positionCount = 13;
|
|
public Material linmaterial;
|
|
|
|
void Awake()
|
|
{
|
|
Instance = this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建电线模型
|
|
/// </summary>
|
|
/// <param name="fatherGo">父物体</param>
|
|
/// <param name="wireName">名称</param>
|
|
/// <param name="start"></param>
|
|
/// <param name="end"></param>
|
|
/// <param name="SagRatio">弧垂比例</param>
|
|
private void CreateNewLineObj(GameObject fatherGo, Vector3 start, Vector3 end, float SagRatio = 0.03f)
|
|
{
|
|
GameObject newLine = new GameObject("line");
|
|
LineRenderer lr = newLine.AddComponent<LineRenderer>();
|
|
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");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 电线弧度的计算方式
|
|
/// </summary>
|
|
/// <param name="index"></param>
|
|
/// <param name="start"></param>
|
|
/// <param name="end"></param>
|
|
/// <param name="SagRatio"></param>
|
|
/// <param name="allNo"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建电线模型
|
|
/// </summary>
|
|
/// <param name="fatherGo">父物体</param>
|
|
/// <param name="wireName">名称</param>
|
|
/// <param name="start"></param>
|
|
/// <param name="end"></param>
|
|
/// <param name="SagRatio">弧垂比例</param>
|
|
public void CreateNewLine(WireObject dw, Vector3 start, Vector3 end, float SagRatio = 0.03f)
|
|
{
|
|
LineRenderer lr = dw.gameObject.GetComponent<LineRenderer>();
|
|
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");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 给lineRender
|
|
/// </summary>
|
|
/// <param name="lr"></param>
|
|
public static void CreateColliderLine(LineRenderer lr, string ChildName)
|
|
{
|
|
return;
|
|
GameObject line = lr.gameObject;
|
|
List<Vector3> point = new List<Vector3>();
|
|
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<CapsuleCollider>();
|
|
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;
|
|
}
|
|
}
|
|
}
|