I was wondering how I would go about making boundaries for the model that I'm working on. I am making a butterfly that flies around randomly. My issue is that it keeps on flying away from the baseplate making it so that it isn't visible. It also flies through parts meaning I can't easily contain it. I was wondering how I would go about setting up boundaries that the butterfly doesn't fly through. I have attempted it so many times and nothing has been working. Here is my latest attempt -
local Body = script.Parent.PrimaryPart -- Defined so I can easily use the CFrame local region = Region3.new(Vector3.new(-270, -8, -270), Vector3.new(270, 100, 270)) -- The region the butterfly can fly in local b1 = game.Workspace.ButterflyBoundaries.Baseplate -- A Directory of folders wher I have the boundaries which are parts. (Not the actual baseplate) local boundary = { b1:WaitForChild("boundary1"), b1:WaitForChild("boundary2"), b1:WaitForChild("boundary3"), b1:WaitForChild("boundary4"), b1:WaitForChild("boundary5"), b1:WaitForChild("boundary6") } -- Each of these refers to each of the 6 parts while true do wait() local partsinregion = workspace:FindPartsInRegion3(region, nil, math.huge) -- Checks if parts are in region for i, part in pairs(partsinregion) do if part.Parent:FindFirstChild("ConfirmedButterfly") then -- Checks if the part is a butterfly (Each butterfly has a part on them named ConfirmedButterfly) script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.Angles(math.rad(math.random(-5,5)), math.rad(math.random(-5,5)), math.rad(math.random(-5,5)))) -- Makes the butterfly face a slightly different direction randomly wait() script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.new(1,0,0)) -- Makes the butterfly fly in the direction its facing end boundary[1].Touched:Connect(function() -- From here on was my latest attempt at making the boundaries, it is also repeated for each part. script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.Angles(math.rad(0), math.rad(180),math.rad(0))) script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.new(3,0,0)) end) boundary[2].Touched:Connect(function() script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.Angles(math.rad(0), math.rad(180),math.rad(0))) script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.new(3,0,0)) end) boundary[3].Touched:Connect(function() script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.Angles(math.rad(0), math.rad(180),math.rad(0))) script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.new(3,0,0)) end) boundary[4].Touched:Connect(function() script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.Angles(math.rad(0), math.rad(180),math.rad(0))) script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.new(3,0,0)) end) boundary[5].Touched:Connect(function() script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.Angles(math.rad(0), math.rad(180),math.rad(0))) script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.new(3,0,0)) end) boundary[6].Touched:Connect(function() script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.Angles(math.rad(0), math.rad(180),math.rad(0))) script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.new(3,0,0)) end) end end
and here is the script without the boundaries
local Body = script.Parent.PrimaryPart -- Defined so I can easily use the CFrame local region = Region3.new(Vector3.new(-270, -8, -270), Vector3.new(270, 100, 270)) -- The region the butterfly can fly in while true do wait() local partsinregion = workspace:FindPartsInRegion3(region, nil, math.huge) -- Checks if parts are in region for i, part in pairs(partsinregion) do if part.Parent:FindFirstChild("ConfirmedButterfly") then -- Checks if the part is a butterfly (Each butterfly has a part on them named ConfirmedButterfly) script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.Angles(math.rad(math.random(-5,5)), math.rad(math.random(-5,5)), math.rad(math.random(-5,5)))) -- Makes the butterfly face a slightly different direction randomly wait() script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.new(1,0,0)) -- Makes the butterfly fly in the direction its facing end end end
If anyone could tell me how I would go about setting up boundaries, or even potentially making it so the butterfly can't fly through parts that would be amazing. Or if there is any resources that could help me that I haven't found yet. I'm fairly new to scripting and this is my first full fledged script I've written. Thank you in advance.
set a path point for your butterfly to move to, before setting that path point use raycasting to ensure no parts are in the way, ensure that the x,y,z of the path point isnt outside that boundary, or just make a region3 and whenever the butterfly moves out of the boundaries, set it back inside.
I was thinking of this problem today. I think the easiest way is to make a new variable that checks whether the movement is outside of the boundaries, let me explain further.
Lets say you want the butterfly to stay within 5,0,5 and 1,0,1 like so:
0, 0, 0, 0, X 0, 0, 0, 0, 0 0, 0, 0, B, 0 0, X, 0, 0, 0 O, 0, 0, 0, 0
O is the origin so 0,0,0. B is the Butterfly, and X marks the boundaries.
You make the butterfly move by changing its CFrame, you already know how to do this. But instead save the movement CFrame by creating a new variable and using an if statement see if the movement goes outside of the bounds.
local Butterfly = workspace.Butterfly while wait(1) do local MovementCFrame = CFrame.new(math.random(-1,1),0,math.random(-2,2)) if (Butterfly.CFrame * MovementCFrame).X > 5 or (Butterfly.CFrame * MovementCFrame).X < 1 or (Butterfly.CFrame * MovementCFrame).Z > 5 or (Butterfly.CFrame * MovementCFrame).Z < 1 then repeat MovementCFrame = CFrame.new(math.random(-1,1),0,math.random(-2,2)) until (Butterfly.CFrame * MovementCFrame).X <= 5 and (Butterfly.CFrame * MovementCFrame).X => 1 and (Butterfly.CFrame * MovementCFrame).Z <= 5 and (Butterfly.CFrame * MovementCFrame).Z => 1 Butterfly.CFrame = Butterfly.CFrame * MovementCFrame
There's something to be said about the grossly long if statement, or whether we can condense this into something smaller. But it'll work and once you get better at scripting you'll be able to condense the loop into under five lines.