Hi! I’m currently trying to make a module that is able to randomly place obstacles such as trees and rocks in a predefined area. I thought about splitting the area into individual “cells” so the obstacles can be randomly offset within them without colliding with other obstacles. However, I don’t really know how to actually define these “cells” within code and how to make the obstacles stay within the bounds of them. Any help would be appreciated.
What I’d do is have a minimum and a maximum range upon each axis (X, Y and Z).
Then you can use the Random datatype to produce a random number in that range.
For example:
local Rand = Random.new() --// No seed, you can give one if you'd like
local XMin, XMax = 10, 20
local YMin, YMax = 0, 10
local ZMin, ZMax = 30, 50
local RandomX = Rand:NextNumber(XMin, XMax)
local RandomY = Rand:NextNumber(YMin, YMax)
local RandomZ = Rand:NextNumber(ZMin, ZMax)
local RandomPosition = Vector3.new(RandomX, RandomY, Random.Z)
Also, a smart way of holding a minimum and a maximum would be to hold it in a NumberRange:
local RandomRange = NumberRange.new(10, 20)
--// Now you can do RandomRange.Min and RandomRange.Max
In my own projects, I often use Parts to define areas sort of like what @return_end1, just easier to modify and fit to the surrounding level. I use CollectionService to get all things tagged with e.g. “Spawner”. To make it general-purpose and reusable, I’d include a “SpawnerInfo” ModuleScript in every Spawner part, holding data about what things can be spawned and how often things should be spawned there. A “SpawnerManager” script can then examine all parts tagged with “Spawner” and spawn a random item from the spawn table however often defined in the ModuleScript. The position of the newly spawn thing is anywhere inside the Spawner part, which you can calculate by adapting the code that @return_end1
posted.