Hi there! My item spawning system is coordinate based and detects collisions for walls and if any objects collide with it and then re-runs if anything collides. the function however I am making this a furnishings store so how would I go about making this near to the wall. I thought I could prehaps use the math.clamp function however I don’t understand it greatly and would not know how to implement this. My code is below for my system so far:
local function SpawnItem()
local Furniture = Furniture:GetChildren()[math.random(1,#Furniture:GetChildren())]
local Part = Furniture:Clone()
Part.Parent = workspace.Chunk.Scenery
local RandomPosition = Vector3.new(Random.new():NextInteger(-175,155),19,Random.new():NextInteger(-30,170))
local Raytoground = Ray.new(RandomPosition,Vector3.new(0,-10000,0))
local PartOnRay,PositionOnRay= workspace:FindPartOnRay(Raytoground)
if PartOnRay and PositionOnRay then
Part:PivotTo(CFrame.new(PositionOnRay) * CFrame.Angles(0,math.random(5,30),0))
local Objects = workspace:GetPartBoundsInBox(Part.PrimaryPart.CFrame,Part.PrimaryPart.Size)
local Parts = Part.PrimaryPart:GetTouchingParts()
for _,v in pairs(Parts) do
if v.Name == "Primary" then
SpawnItem()
Part:Destroy()
return false
end
end
for _,v in pairs(Objects) do
if v.Material == Enum.Material.Brick or v.Material == Enum.Material.Concrete then
SpawnItem()
Part:Destroy()
return false
end
end
end
end
Previously you were using a ray in order to detect where a wall was. By using this information, along with where the ray was fired from (it’s direction) and the size of the furniture (bounding box) you should be able to put it next to the wall.
(Ex: If you cast a ray from the front of the wall to the wall, use the position and offset the model in the direction of the ray origin by the size of the furniture in that direction you should have it “hug” the wall)
Hi again, so I added a check to see which wall is the nearest and I found it, how can I find out where to place this with the RayCast? Here is my attempt so far:
for i,v in pairs(workspace.Chunk.Walls:GetDescendants()) do
if v:IsA("BasePart") then
if v.Material == Enum.Material.Brick then
if (PositionOnRay - v.Position).Magnitude <= 100 then
if Closest == nil then
Closest = v
else
if (PositionOnRay- v.Position).magnitude < (Closest.Position - v.Position).magnitude then
Closest = v
end
end
end
end
end
end
Part:PivotTo(CFrame.new(Closest.CFrame.X - 3,Part:GetPivot().Y,Closest.CFrame.Y - 3) * CFrame.Angles(0,Closest.Orientation.Y,0))
end
Edit: Could I use the following code to do what I have asked?
local function SpawnItem()
local Furniture = Furniture:GetChildren()[math.random(1,#Furniture:GetChildren())]
local Part = Furniture:Clone()
Part.Parent = workspace.Chunk.Scenery
local RandomPosition = Vector3.new(Random.new():NextInteger(-175,155),19,Random.new():NextInteger(-30,170))
local Raytoground = Ray.new(RandomPosition,Vector3.new(0,-10000,0))
local PartOnRay,PositionOnRay= workspace:FindPartOnRay(Raytoground)
if PartOnRay and PositionOnRay then
Part:PivotTo(CFrame.new(PositionOnRay) * CFrame.Angles(0,math.random(5,30),0))
local Objects = workspace:GetPartBoundsInBox(Part.PrimaryPart.CFrame,Part.PrimaryPart.Size)
local Parts = Part.PrimaryPart:GetTouchingParts()
for _,v in pairs(Parts) do
if v.Name == "Primary" then
SpawnItem()
Part:Destroy()
return false
end
end
for _,v in pairs(Objects) do
if v.Material == Enum.Material.Brick or v.Material == Enum.Material.Concrete then
SpawnItem()
Part:Destroy()
return false
end
end
end
end
local function SpawnItem()
local Furniture = Furniture:GetChildren()[math.random(1,#Furniture:GetChildren())]
local Part = Furniture:Clone()
Part.Parent = workspace.Chunk.Scenery
local RandomPosition = Vector3.new(Random.new():NextInteger(-175,155),19,Random.new():NextInteger(-30,170))
local Raytoground = Ray.new(