2017-01-25 8 views
2

私は、カーブフィーバーのようなゲームを作成しようとしています。私が達成しようとしているのは、1秒に1回、ラインの間にギャップがあることです。Unityは、ポイント間のギャップを持つラインを作成します。

現在、私はLineRendererを使用して、このようにポイントを設定しています:

void Update() { 
    if(Vector3.Distance(points.Last(), snake.position) > pointSpacing) 
     SetPoint(); 
} 

public void SetPoint(){ 
    if (noGap) 
    { 
     if (points.Count > 1) 
      coll.points = points.ToArray<Vector2>(); 

     points.Add(snake.position); 

     line.numPositions = points.Count; 
     line.SetPosition(points.Count - 1, snake.position); 
    } 

} 

public IEnumerator LineGap(){ 
    while (enabled) 
    { 
     yield return new WaitForSeconds(2f); 
     noGap = false; 
     yield return new WaitForSeconds(.5f); 
     noGap = true; 
    } 
} 

コルーチンで、私は、0.5秒間2秒に1回ポイントを作成しないようにしようとしたが、LineRendererを作成するために作られている上2点毎の線。

私がしようとしていることを達成する方法はありますか?たぶん別の種類のレンダラーを使用していますか?

+0

Vectrosityを調べましたか? – maksymiuk

+1

もちろん、ラインをレンダリングする別の方法があります! ['GL.Lines'](https://docs.unity3d.com/ScriptReference/GL.LINES.html)を検討してください。または、1つではない厚さの場合は、クワッドを描く必要があります。 – Serlite

+0

複数のラインレンダラを使用します。ラインの各セグメントに1つずつ使用します。絶対に必要でない限り、低レベルのレンダリング機能を使用しないでください。 – Arshia001

答えて

1

1つのオプションは、各線分に新しいLineRendererを作成することです。あなたのヘビはLineRenderersのListを追跡します。 LineRenderer GameObjectをプレハブにすると、即座にそれを生成することができます。

public class Snake 
{ 
    public GameObject LinePrefab;   //Prefab gameobject with LineRenderer 
    private List<LineRenderer> pathList; //List of line segments 
    private LineRenderer line;    //Current line 


    private void Start(){ 
     this.pathList = new List<LineRenderer>(); 
     SpawnNewLineSegment(); 

     //Other initialization code 
    } 

    public void Update(){ 
     if (!noGap && Vector3.Distance(points.Last(), snake.position) > pointSpacing) 
      SetPoint() 
    } 

    public void SetPoint(){ 
     if (points.Count > 1) 
      coll.points = points.ToArray<Vector2>(); 

     points.Add(snake.position); 

     //Increment the number of points in the line 
     line.numPositions = line.numPositions + 1; 
     line.SetPosition(line.numPositions - 1, snake.position); 
    } 

    public IEnumerator LineGap(){ 
     while (enabled) 
     { 
      yield return new WaitForSeconds(2f); 
      noGap = false; 
      yield return new WaitForSeconds(.5f); 
      noGap = true; 

      //We are starting a new line, create a new line segment 
      SpawnNewLineSegment(); 
     } 

    private LineRenderer SpawnNewLineSegment(){ 
     //Spawn the gameobject as a parent of this object 
     GameObject go = Instantiate(this.LinePrefab, this.transform); 
     this.line = go.GetComponent<LineRenderer>(); 
     this.pathList.Add(this.line) 

     //Set the first point on the line 
     SetPoint() 
    } 
} 

唯一の問題は、あなたの衝突を処理する方法になります(私はcollが何を意味するのかであると仮定):あなたのSnakeクラスには、次のようになります。衝突が隙間に及ぶか、そこに穴が残っていますか?書かれているように、コライダーは連続している(ギャップがない)ように見えます。

新しいGameObjectを実行時に生成するパフォーマンスが心配な場合は、初期化時にプールを作成し、必要になるまで無効にすることができます。

+0

答えをありがとう!私は結局、新しいLineRendererを実際にインスタンス化しなければならないと考えていたので、現在は動作しています。しかし、これは質問に答えるので私は答えとしてそれを受け入れます! –

関連する問題