Hi, im trying to use a big square part to spawn parts on the ground. How can i randomize the ray origin position on the X and Z axis?
My current script: (this script sends a ray towards the ground and spawns a part, the problem is that the ray comes from the middle of the part)
local spawner = script.Parent
while wait(0.1) do
local rayOrigin = spawner.Position
local rayDirection = Vector3.new(0, -200, 0)
local raycastParams = RaycastParams.new()
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
if raycastResult then
local part = Instance.new(“Part”, workspace)
part.Position = raycastResult.Position
part.Color = Color3.fromRGB(255, 0, 0)
part.Anchored = true
print(“spawned”)
end
end
Randomizing ray origin is very simple. Use math.random.
local x = (your number)
local y = math.random(1, 50) -- Or some other 2 numbers
local z = math.random (1, 50) -- Or some other 2 numbers
local rayOrigin = Vector3.new(x,y,z)
If this works, please list as the Solution! Thanks!
I know how to randomize numbers the problem is that i don’t know how to get a random position from that part. I only want rays to be sent from that part.
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
This is the full script
local spawner = script.Parent
function getRandomInPart(spawner)
local random = Random.new()
local randomCFrame = spawner.CFrame * CFrame.new(random:NextNumber(-spawner.Size.X/2,spawner.Size.X/2), random:NextNumber(-spawner.Size.Y/2,spawner.Size.Y/2), random:NextNumber(-spawner.Size.Z/2,spawner.Size.Z/2))
return randomCFrame
end
while wait(0.1) do
local rayOrigin = getRandomInPart(spawner)
local rayDirection = Vector3.new(0, -200, 0)
local raycastParams = RaycastParams.new()
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
if raycastResult then
local part = Instance.new("Part", workspace)
part.Position = raycastResult.Position
part.Color = Color3.fromRGB(255, 0, 0)
part.Anchored = true
print("spawned")
end
end
Yeah, i switched to Vector3 i dont get any error but the script also doesn’t work
This is the new getRandomInPart function
function getRandomInPart(spawner)
local random = Random.new()
local randomPosition = spawner.Position * Vector3.new(random:NextNumber(-spawner.Size.X/2,spawner.Size.X/2), random:NextNumber(-spawner.Size.Y/2,spawner.Size.Y/2), random:NextNumber(-spawner.Size.Z/2,spawner.Size.Z/2))
return randomPosition
end
local Region = workspace.Region
local x = Region.Position.x
local z = Region.Position.Z
while wait(0.1) do
local xS = Region.Size.X/2
local zS = Region.Size.Z/2
local pos1 = math.random(x-xS,x+xS)
local pos2 = math.random(z-zS,z+zS)
local rayOrigin = Vector3.new(pos1, Region.Position.Y, pos2)
local rayDirection = Vector3.new(0, -500, 0)
local raycastParams = RaycastParams.new()
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
if raycastResult then
local part = Instance.new("Part", workspace)
part.Position = raycastResult.Position
part.Color = Color3.fromRGB(255, 0, 0)
part.Anchored = true
print("spawned")
end
end