52 lines
1.4 KiB
C#
52 lines
1.4 KiB
C#
using UnityEngine;
|
|
|
|
namespace Models
|
|
{
|
|
public class Bonds
|
|
{
|
|
public GameObject start;
|
|
public GameObject end;
|
|
private GameObject bond;
|
|
|
|
|
|
public Bonds(GameObject start,GameObject end,float scale)
|
|
{
|
|
this.start = start;
|
|
this.end = end;
|
|
|
|
bond = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
|
|
|
|
bond.transform.localScale = new Vector3(scale/2, scale/2, scale/2);
|
|
|
|
//这里可以设置材质,具体自己设置
|
|
bond.GetComponent<Renderer>().material.color = Color.cyan;
|
|
}
|
|
|
|
|
|
public void UpdateBond()
|
|
{
|
|
var startpos = start.transform.position;
|
|
var endpos = end.transform.position;
|
|
Vector3 rightPosition = (startpos + endpos) / 2;
|
|
Vector3 rightRotation = endpos - startpos;
|
|
float HalfLength = Vector3.Distance(startpos, endpos) / 2;
|
|
float LThickness = 0.1f;//线的粗细
|
|
|
|
//创建圆柱体
|
|
|
|
//bond.gameObject.transform.parent = transform;
|
|
bond.transform.position = rightPosition;
|
|
bond.transform.rotation = Quaternion.FromToRotation(Vector3.up, rightRotation);
|
|
bond.transform.localScale = new Vector3(LThickness, HalfLength, LThickness);
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} |