2017-10-11 51 views
-6

如何在統一5中創建這種類型的斜坡/地形?如何在統一的斜坡/地形?

我正在創建一個遊戲,我需要隨機的山坡。我不需要山內的細節,只需要它的邊界。 我很困惑我是否必須通過代碼創建這種類型的斜坡,或者是否有統一的任何直接特徵來製作隨機山脈邊界。 ​​

回答

0

您可以在程序上創建類似的東西。我剛剛創建了一個行爲,將創建一個senoidal軌跡就像上面的圖片:

// written by 'imerso' as a StackOverflow answer. 
using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class SlopeTerrain : MonoBehaviour 
{ 
    public int heightInMeters = 5; 
    public int widthInMeters = 128; 
    public float ondulationFactor = 0.1f; 
    public Material material; 

    // Use this for initialization 
    void Start() 
    { 
     Mesh mesh = new Mesh(); 
     List<Vector3> vertices = new List<Vector3>(); 
     List<int> triangles = new List<int>(); 

     for (int p = 0; p < widthInMeters; p++) 
     { 
      // add two vertices, one at the horizontal position but displaced by a sine wave, 
      // and other at the same horizontal position, but at bottom 
      vertices.Add(new Vector3(p, Mathf.Abs(heightInMeters * Mathf.Sin(p * ondulationFactor)), 0)); 
      vertices.Add(new Vector3(p, 0, 0)); 

      if (p > 0) 
      { 
       // we have enough vertices created already, 
       // so start creating triangles using the previous vertices indices 
       int v0 = p * 2 - 2;   // first sine vertex 
       int v1 = p * 2 - 1;   // first bottom vertex 
       int v2 = p * 2;    // second sine vertex 
       int v3 = p * 2 + 1;   // second bottom vertex 

       // first triangle 
       triangles.Add(v0); 
       triangles.Add(v1); 
       triangles.Add(v2); 

       // second triangle 
       triangles.Add(v2); 
       triangles.Add(v1); 
       triangles.Add(v3); 
      } 
     } 

     mesh.vertices = vertices.ToArray(); 
     mesh.triangles = triangles.ToArray(); 

     mesh.RecalculateBounds(); 

     MeshRenderer r = gameObject.AddComponent<MeshRenderer>(); 
     MeshFilter f = gameObject.AddComponent<MeshFilter>(); 

     if (material != null) 
     { 
      r.sharedMaterial = material; 
     } 

     f.sharedMesh = mesh; 
    } 
} 

爲了保持它的簡單讓你瞭解,雖然,我做它只是2D。要使用它,請創建一個空的GameObject,然後將上面的腳本放在上面。它有一些你可以調整的參數。祝你好運。