Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Preventing parts from moving outside of brick?

Asked by 6 years ago

Ok, so I know the title may seem like this is something very basic, but I have reached a problem that I have no idea how to solve; I have a fish that is swimming in a pond, it does this by generating random Vector3positions and CFramelerping towards them. However, I want to implement a feature to prevent the fish from just plain swimming through parts and out of the map.

Is there a way in rbx.lua that would let me find the positions of the edges of parts, or does anyone know of an easier method that would let me achieve the same thing?

If anyone want's to see the script, here it is:

while wait() do

local part = script.Parent
marker = Vector3.new(128.015, 2.109, 95.71)

local pos = part.CFrame
local increment = Vector3.new(math.random(-10,10),0,math.random(-10,10))



local newPos = part.CFrame + increment


for i = 0,1,0.01 do
    part.CFrame = pos:lerp(newPos, i)
    part.CFrame = CFrame.new(part.CFrame.p, increment)
    wait(0.01)
end



end

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

I'm not really sure how you would do the edges but for the part to move, here you go.

Add a border, then insert this into each of the bricks in the border.

script.Parent.Touched:connect(function(part)
    local pos = part.CFrame
    local increment = Vector3.new(math.random(-10,10),0,math.random(-10,10))
    local newPos = part.CFrame + increment
    for i, v in pairs(script.Parent:getChildren()) do
        if v:IsA("BasePart") and Name ~= "Baseplate" then
            for i = 0, 1, 0.01 do
                part.CFrame = pos:lerp(newPos, i)
                part.CFrame = CFrame.new(part.CFrame.p, increment)
                wait(0.01)
            end 
        end
    end
end

Hope this helped!

Ad

Answer this question