Random terrain

From Legacy Roblox Wiki
Revision as of 02:23, 9 July 2011 by >Sduke524 (New page: == Introduction == Procedurally generated terrain, often refered to as randomly generated terrain, is a terrain that is created via script rather than by hand. This creates the ability to ...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Introduction

Procedurally generated terrain, often refered to as randomly generated terrain, is a terrain that is created via script rather than by hand. This creates the ability to have games that are a completely different experience every time as well as use alot less work.

The most basic form

The most basic algorithm, or steps used to solve a certain problem, is to place bricks of random size in random positions across your baseplate. To do this we will set up a simple for loop that instances a new, anchored part every time.

for i=1,200 do
    local a=Instance.new("Part",workspace)--creates new part in workspace
    a.Anchored=true

Next we will set the size to a random amount. For this we will use the function math.random()

    a.Size=Vector3.new(math.random(1,20),math.random(1,20),math.random(1,20))

This will set the Size to a random amount be 1 and 20 for the X,Y and Z size. Now for the last step of our algorithm, use CFrame to place the brick in a random spot. We will easily just place the X and Z axis using math.random() yet for the Y axis, we'll have to do something special. Being that we want all the bricks to stay on the ground, we want their base to be at 0. Now being that the Position of a brick in roblox uses the center of the brick, we'll have to divide the Y size in half, so that it sits just perfectly on 0. So it would be :

    a.CFrame=CFrame.new(math.random(-100,100),a.Size.Y/2,math.random(-100,100))
end -- for the for loop