How to make bricks randomly come out of walls

I want to make parts randomly appear off of the wall, like this:

but how would I make this in a script, so it always touches the wall.

Something like this might work.

while wait(t) do
  local part = Instance.new("Basepart")
  part.Parent = workspace
  
  local mid =  PrimaryPart.Position + Part.Size.Z/2 + PrimaryPart.Size.Z/2
  local newpoint = Vector3.new(math.random(PrimaryPart.Position.X-PrimaryPart.Position.X/2, PrimaryPart.Position.X+PrimaryPart.Position.X/2), math.random(PrimaryPart.Position.Y-PrimaryPart.Position.Y/2, PrimaryPart.Position.Y+PrimaryPart.Position.Y/2), 0)
  local final = CFrame.new(mid)*newpoint
  
  part.CFrame = final
end

Logic is that you try to add half depth to central point of wall. You then math.random points on the X and Y axis, since this now essentially becomes a 2D operation. Obviously completely depends on what axis you have as depth.

This is how I would approach it:

Set the position of brick to the position of your targeted wall
Add half the size of the wall of one axis to the brick position
Subtract the brick position by half the brick size + 0.05 to avoid zfighting
Add some random number to the position of the other 2 axis within the size of the wall

Lastly, tween the brick in a way that you will add the bricks position by half the brick size + 0.05

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.