2017-10-11 66 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,然后将上面的脚本放在上面。它有一些你可以调整的参数。祝你好运。