using UnityEngine; namespace Models { /// /// 捕捉点之间的连接线 /// public class Bond { protected readonly GameObject Start; protected readonly GameObject End; private readonly GameObject _bond; public Quaternion Rotation => _bond.transform.rotation; public Bond(GameObject start,GameObject end,float scale) { Start = start; End = end; _bond = GameObject.CreatePrimitive(PrimitiveType.Cylinder); _bond.transform.localScale = new Vector3(scale/2, scale/2, scale/2); //这里可以设置材质,具体自己设置 _bond.GetComponent().material.color = Color.cyan; } public void UpdateBond() { var startPos = Start.transform.position; var endPos = End.transform.position; var rightPosition = (startPos + endPos) / 2; var rightRotation = endPos - startPos; var halfLength = Vector3.Distance(startPos, endPos) / 2; var thickness = 0.1f;//线的粗细 //创建圆柱体 //bond.gameObject.transform.parent = transform; _bond.transform.position = rightPosition; _bond.transform.rotation = Quaternion.FromToRotation(Vector3.up, rightRotation); _bond.transform.localScale = new Vector3(thickness, halfLength, thickness); } } }