【问题标题】:Put and duplicate Gameobject along or accross a path Unity沿路径或跨路径放置和复制游戏对象 Unity
【发布时间】:2017-06-13 13:43:35
【问题描述】:

我是 Unity 的初学者,我想独自创建一个个人项目。 我在场景中有两个游戏对象,我在 Unity 上使用 LineRenderer 和 NavMesh 代理在两个游戏对象之间绘制路径,到目前为止,一切都很好,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 现在我的问题是如何放置和复制游戏对象,例如沿着这条路径或穿过这条路径的立方体? Here photo the result which i want to have

我使用 C# 脚本,Unity 5.6

【问题讨论】:

    标签: c# unity3d navigation


    【解决方案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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    相关资源
    最近更新 更多