Sep
2
2012

Flying inside 3D Perlin Cave

Never-ending perlin caves to explore!

Current features:
– Unlimited cave flying
– Plasma(?) cannon
– Moving around with keys (wasd/arrow keys)
– Shooting with mousebutton
– 1 stone material
– added: light source, shooting sound, block destruction, basic collision detection for movement (not for player destruction yet)

I’m thinking something like this, maybe another version with the cave in 2.5D and view from side:
Scramble C64 (Play Online: http://c64s.com/game/689/scramble/ | Video: http://www.youtube.com/watch?v=qqBVDX3xy0M )

Webplayer:
http://unitycoder.com/upload/demos/UnlimitedPerlinCaves3D1/

PerlinCaveTile.cs (attach this to empty game object, then assign the block prefab to it


using UnityEngine;
using System.Collections;

public class PerlinCaveTiles : MonoBehaviour {

public int width=16;
public int height=16;

public float fireRate = 0.5f;
private float nextFire = 0.0f;

public float noiseScale=11.5f;
public float depthNoiseScale=1.0f;
private float z=0.0f;
//    private float worldy=0.0f;

private float value;
public float holeThreshHold=0.2f;

public GameObject block; // default block

private Perlin perlin;
Perlin p = new Perlin();

// Use this for initialization
void Start ()
{

}

// Update is called once per frame
void Update ()
{
if (Time.time > nextFire)
{
CreateChunk();
nextFire = Time.time + fireRate;
}
}

void CreateChunk()
{
// generate initial map`/wall?
// create map array
//var n:int=0;

for (int y = 0;y<height;y++)
{
Instantiate(block, transform.position+new Vector3(0,y,0), transform.rotation);
Instantiate(block, transform.position+new Vector3(width,y,0), transform.rotation);
}

for (int x = 0;x<width;x++)
{
Instantiate(block, transform.position+new Vector3(x,0,0), transform.rotation);
Instantiate(block, transform.position+new Vector3(x,height,0), transform.rotation);
}

for (int y = 1;y<height;y++)
{
for (int x = 1;x<width;x++)
{

value = p.Noise(x*noiseScale,y*noiseScale,z*depthNoiseScale); // default

if (value>holeThreshHold)
{

Instantiate(block, transform.position+new Vector3(x,y,0), transform.rotation);

}

}

}

z++;

}

}

BlockMover.cs (temporary script for moving the blocks.. attach to block prefab)


using UnityEngine;
using System.Collections;

public class BlockMover : MonoBehaviour {

float speed = 3;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
transform.Translate(-Vector3.forward*speed*Time.deltaTime, Space.World);

if (transform.position.z < -17)
{
Destroy (gameObject);
}

}
}


9 Comments + Add Comment

  • Any infomation about the source release?

  • Yeah! I’d love to see the source!

  • yes, its coming eventually, bit busy lately 🙂

    and actually all that this does:
    – if Mathf.Perlin() > threshold, then instantiate cube..
    – move cubes in z axis, until behind camera, then destroy it

    that makes it bit slow, should rather use custom meshes..(similar to voxel terrains)

  • Can’t wait! I’m interested to see how you’ve created 3D noise with unity’s mathf implementation.

  • hi! this looks very intresting! i would really need to know how you did it. could you please publish at least the scene you already have? i would be very thankful!! 🙂

    • added couple sources on the post (it would really need to be re-done, it starts to get too slow because of too many cubes, object pooling should be used also if use invidual cubes)

      • ooh, soo great! thank you very much!! 🙂

  • Error on the first script, ‘The type of Namespace ‘Perlin’ could not be found’ on lines 22 and 23. Any help?

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Youtube Join Discord Twitch

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.