2017-08-10 14 views
0

私はunity 3dを初めて使いました。私は非常に単純な障害物コースゲームを作りたいと思います。私はそれが複数のレベルを持つことを望んでいませんが、代わりに、誰かがゲームを開始するたびに無作為に生成するシーンが1つしかありません。ランダムに生成されたワールドポイントでオブジェクトを生成する

これは良いアイデアを説明するための画像です:アプリケーションが起動し、プレイヤーは唯一のギャップを介して取得することができますたびに生成されます壁があるだろう、各強調表示されたセクションでは

enter image description here

を各セクションの領域a、b、cのいずれかにランダムに生成されます。 これを見てみましたが、この例はあまりありませんでした。

ご不明な点がございましたら、お気軽にお問い合わせください。私はいつもレスポンスを知らされています。

ありがとうございました!

答えて

5

基本的な考え方:

  1. (など各壁の間の距離、可能な位置、)パラメータのカップルとスクリプト(例えばWallSpawner)を作成し、それを添付して、あなたの障害物
  2. からプレハブを作成します。あなたのシーンのオブジェクト(例えば、あなたのケースではWalls)。
  3. StartまたはAwakeの方法では、プレハブのコピーをInstantiateで作成し、ランダムに選択した位置に渡します。

スクリプト例:

public class WallSpawner : MonoBehaviour 
{ 
    // Prefab 
    public GameObject ObstaclePrefab; 

    // Origin point (first row, first obstacle) 
    public Vector3 Origin; 

    // "distance" between two rows 
    public Vector3 VectorPerRow; 

    // "distance" between two obstacles (wall segments) 
    public Vector3 VectorPerObstacle; 

    // How many rows to spawn 
    public int RowsToSpawn; 

    // How many obstacles per row (including the one we skip for the gap) 
    public int ObstaclesPerRow; 

    void Start() 
    { 
     Random r = new Random(); 

     // loop through all rows 
     for (int row = 0; row < RowsToSpawn; row++) 
     { 
      // randomly select a location for the gap 
      int gap = r.Next(ObstaclesPerRow); 
      for (int column = 0; column < ObstaclesPerRow; column++) 
      { 
       if (column == gap) continue; 

       // calculate position 
       Vector3 spawnPosition = Origin + (VectorPerRow * row) + (VectorPerObstacle * column); 
       // create new obstacle 
       GameObject newObstacle = Instantiate(ObstaclePrefab, spawnPosition, Quaternion.identity); 
       // attach it to the current game object 
       newObstacle.transform.parent = transform; 
      } 
     } 
    } 
} 

例パラメータ:

Parameters in the Editor

例の結果:

Example Walls (5 per row)

+0

どうもありがとうございました。これでどれくらいの時間がかかったか分かりません。ご迷惑をおかけして申し訳ありませんが、本当にありがとうございます。再びありがとう!今はそんなに馬鹿だと感じる。 –

+0

@PascalGamingYTPascalPlayz問題ありません。これがあなたの質問に答えるなら、私の質問の左上にあるチェックマークをクリックして回答としてマークし、質問を閉じる。 –

関連する問題