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

How do I set up a barrier for a movable part in CFrame?

Asked by 2 years ago

I'm making a snake game, my snake is able to be moved out of its bounds. How can I prevent the player from moving the snake out of its 40x24 environment?

while true do
        print(part.CFrame)
        wait(1)
    end

if part.CFrame == (0,0,12) then
    part.CFrame = CFrame.new (0,0,11)
end

2 answers

Log in to vote
1
Answered by 2 years ago
Edited 2 years ago

Assuming your grid starts at (0,0,0) and ends at (0,40,24), I would add an if statement after each movement of the snake to check if it exceeds a particular coordinate. Here's an example that would disallow movement.

if part.CFrame.Y > 40 then
    part.CFrame.Y = 40
elseif part.CFrame.Y < 0 then
    part.CFrame.Y = 0
end



if part.CFrame.Z > 24 then
    part.CFrame.Z = 24
elseif part.CFrame.Z < 0 then
    part.CFrame.Z = 0
end

edit: try this

RunService.RenderStepped:Connect(function()
    camera.CameraSubject = part
    camera.CameraType = Enum.CameraType.Attach

    local move = CFrame.new(0,0,0)
    if down.W == true then
        move = move * CFrame.new(0,0,-.5)
        if part.CFrame.Z <= -11 then
            move = move * CFrame.new(0,0,0)
        end
    end
    if down.S == true then
        move = move * CFrame.new(0,0,.5)
        if part.CFrame.Z >= 11 then
            move = move * CFrame.new(0,0,0)
        end
    end
    if down.A == true then
        move = move * CFrame.new(-.5,0,0)
        if part.CFrame.X <= -19 then
            move = move * CFrame.new(0,0,0)
        end
    end
    if down.D == true then
        move = move * CFrame.new(.5,0,0)
        if part.CFrame.X >= 19 then
            move = move * CFrame.new(0,0,0)
        end
    end
    part.CFrame = part.CFrame * move

end)
0
Worked! I had to change the second move line to .5 and -.5 respectively to make it stay in bounds. Thank you. Xenevious 19 — 2y
Ad
Log in to vote
1
Answered by 2 years ago

The corners are as follows, (19,0,11)(-19,0,11)(19,0,-11)(-19,0,-11). I plugged these numbers into the script you sent, and now I'm having trouble where to put them as a whole.

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local LP = Players.LocalPlayer
local mouse = LP:GetMouse()
local camera = workspace.CurrentCamera

local part = game.Workspace.SNAKE
part.CFrame = CFrame.new(0,.1,0)

local down = {
    W = false,
    A = false,
    S = false,
    D = false
}

mouse.KeyDown:Connect(function(key)
    key = string.lower(key)
    if key == "w" then
        down.W = true
    elseif key == "s" then
        down.S = true
    end
end)

mouse.KeyDown:Connect(function(key)
    key = string.lower(key)
    if key == "a" then
        down.A = true
    elseif key == "d" then
        down.D = true
    end
end)

mouse.KeyUp:Connect(function(key)
    key = string.lower(key)
    if key == "w" then
        down.W = false
    elseif key == "s" then
        down.S = false
    end
end)

mouse.KeyUp:Connect(function(key)
    key = string.lower(key)
    if key == "a" then
        down.A = false
    elseif key == "d" then
        down.D = false
    end
end)

RunService.RenderStepped:Connect(function()
    camera.CameraSubject = part
    camera.CameraType = Enum.CameraType.Attach

    local move = CFrame.new(0,0,0)
    if down.W == true then
        move = move * CFrame.new(0,0,-.5)
    end
    if down.S == true then
        move = move * CFrame.new(0,0,.5)
    end
    if down.A == true then
        move = move * CFrame.new(-.5,0,0)
    end
    if down.D == true then
        move = move * CFrame.new(.5,0,0)
    end
    part.CFrame = part.CFrame * move

end)
0
I edited my answer, try using the code I put in there brandons2005 87 — 2y

Answer this question