首页 文章

放置并复制Gameobject沿着或沿着Unity路径移动

提问于
浏览
0

我是Unity的初学者,我想独自创建一个个人项目 . 我在场景中有两个Gameobjects,我在Unity上使用LineRenderer和NavMesh代理来绘制两个游戏对象之间的路径,到目前为止,一切都很好,两个Gameobjects之间的Unity绘制路径 .

Void Update (){
    OnDrawGiz(GameObject.Find("Destination"));
  }
  void OnDrawGiz(GameObject obj)
   {

    Transform target = obj.GetComponent<Transform> ();
    NavMeshAgent nav;
    LineRenderer line;
    nav= this.GetComponent<NavMeshAgent>();
    if( nav == null || nav.path == null )
        return;
    line = this.GetComponent<LineRenderer>();
    line.material = new Material( Shader.Find( "Sprites/Default" ) ) { color 
         = Color.yellow };

    line.startColor=Color.yellow;
    line.endColor = Color.green;

    nav.SetDestination (target.position);
    nav.isStopped=true;
    var path = nav.path;
    if (path.corners.Length < 2)
        return ;

    line.positionCount=path.corners.Length ;

    //Draw the line
    for( int i = 0; i < path.corners.Length; i++ )
    {
        line.SetPosition( i, path.corners[i] );
    }
}

darw path现在我的问题是如何放置和复制一个Gameobject,例如沿着这条路径或跨越这条路径的立方体? Here photo the result which i want to have

我使用C#脚本,Unity 5.6

1 回答

  • 1

    for 循环中,在 line.SetPosition( i, path.corners[i] ); 之前或之后,您可以使用path.corners [i]来获取沿线的顶点 . 只需使用Instantiate在那里实例化一个游戏对象 . 例如:

    Instantiate(prefab, path.corners[i], Quaternion.identity);

    查看此处的文档(第一个示例与您想要的类似):https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

相关问题