3D空间 圆柱体画线

//添加一个圆柱体

//添加多个Cube

//切记:脚本不能绑定在圆柱体上,会因为不断调用自身而造成死循环。

using UnityEngine;
using System.Collections;

public class StartDrawLine : MonoBehaviour
{
    public GameObject Line;//圆柱体用于画线条;
    public Transform[] WayPoints;
    int i = 0;
void Update() { if (i < WayPoints.Length - 1) { Vector3 tempPos = (WayPoints[i].position + WayPoints[i + 1].position) / 2;//计算两个点的中间点坐标 GameObject go = (GameObject)Instantiate(Line, tempPos, Quaternion.identity);//在物体的中点处实例化,物体的缩放从中间向两侧延伸 go.name = "" + i; go.transform.right = (go.transform.position - WayPoints[i].position).normalized;//改变线条的朝向 float distance = Vector3.Distance(WayPoints[i].position, WayPoints[i + 1].position);//计算两点间的距离 go.transform.localScale = new Vector3(distance, 0.01f, 0.01f);//延长线条连接两点 i++; Debug.Log(i); } } }
原文地址:https://www.cnblogs.com/Cocomo/p/5755531.html