No description, website, or topics provided.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Assets
Packages
ProjectSettings
.gitignore
README.md

README.md

DynamicCuboidTerrain

Installation/Usage

Copy CuboidTerrain.cs somewhere into your assets or assets/scripts folder

Delete the following line, (it shifts the terrain by a linear speed in the x direction; used for testing purposes)

#define TESTING

Create a script file that extends CuboidTerrain

using nz.Rishaan.DynamicCuboidTerrain
public class MyTerrain : CuboidTerrain {
  //Code here
}

Override up to 3 of these methods:

Apply settings on initialization

Should always be overriden

public void Settings()

Create initial height map

Usually won't need to be overriden

public void InitializeMap()

This should initialize public float[,] map and create the initial height map inside of it.

Update height map to represent change in terrain or player movement

Usually won't need to be overriden

public void UpdateHeightMap()

If the movement you want to do is fairly simple, don't override these two functions, instead alter the float values playerX and playerZ to represent the players location relative to the starting point of the terrain.

Attach your script to a GameObject

And set the Terrain Cube Prefab to the prefab that the terrain cuboids should be made of. This should be primitive 1x1x1 unit cube with your own materials, shaders etc.

Image Failed To Load


Template

A typical implementation, assuming both classes are attached to the same game object.

using nz.Rishaan.DynamicCuboidTerrain;

public class MyTerrain : CuboidTerrain {
  public override void Settings () {
  cent = true; //center the map at 0,-,0
            cent0 = true; //set the top of the center cube of the terrain to y=0
            stretch = true; //stretch cubes
            stretchTo = -4f; //stretch cubes to y = stretchTo
            asymp = true; //Use asymptotic averaging to smooth terrain
            asympAmountPerSec = 0.99f;
            maxHeight = 2;
            minHeight = -2;
            seedX = Random.Range(0, 100);
            seedZ = Random.Range(0, 100);
            X = 10; //X Cubes
            Z = 10; //Z Cubes
  }
}
-
using UnityEngine;
public class PlayerMovement : MonoBehaviour {

  MyTerrain terrain;
  
  void Start () {
    terrain = this.GetComponent<MyTerrain>();
  }
  
  void Update () {
    if (//PlayerMovement//) {
      terrain.playerX += ...;
      terrain.playerZ += ...;
    }
  }
}

Below is the code of the 3 virtual methods that can be overrided, this is what will be run if they aren't overriden.

        public virtual void Settings()
        {
            cent = true; //center the map at 0,-,0
            cent0 = true; //set the top of the center cube of the terrain to y=0
            stretch = true; //stretch cubes
            stretchTo = -4f; //stretch cubes to y = stretchTo
            asymp = true; //Use asymptotic averaging to smooth terrain
            asympAmountPerSec = 0.99f;
            maxHeight = 2;
            minHeight = -2;
            seedX = Random.Range(0, 100);
            seedZ = Random.Range(0, 100);
            X = 10; //X Cubes
            Z = 10; //Z Cubes
        }

        //Override this
        //  To create a custom random or crafted map
        public virtual void InitializeMap()
        {
            map = new float[X, Z];
            float heightRange = maxHeight - minHeight;
            for (int x = 0; x < X; ++x) for (int z = 0; z < Z; ++z)
                    map[x, z] = minHeight + heightRange * Mathf.PerlinNoise(seedX + x * 0.1f, seedZ + z * 0.1f);
        }

        //Override this
        //  To shift/update the height map according to player movement
        public virtual void UpdateHeightMap()
        {

            float heightRange = maxHeight - minHeight;
            for (int x = 0; x < X; ++x) for (int z = 0; z < Z; ++z)
                    map[x, z] = minHeight + heightRange * Mathf.PerlinNoise(seedX + (x + playerX) * 0.1f, seedZ + (z + playerZ) * 0.1f);
        }

Source

CuboidTerrain.cs