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

How to make my 2D Movement Script prevent players from going off map?

Asked by 1 year ago

Hello,

I have a 2D movement system for a game like Space Invaders, and I don't want the player to go off map. Does anyone know how I can do that? I already have a script, the movement works, but the player can still go off the map!

UIS.InputBegan:Connect(function(input, GameProcessedEvent)
    if input.KeyCode == Enum.KeyCode.D then
        if HRP.CFrame ~= Border3.CFrame then
            canMove = true
            HRP.CFrame = HRP.CFrame * CFrame.new(10, 0, 0)
        end
        if HRP.CFrame == Border.CFrame then
            canMove = false
        end
    elseif input.KeyCode == Enum.KeyCode.A then
        HRP.CFrame = HRP.CFrame * CFrame.new(-10, 0, 0)
        if HRP.CFrame == Border2.CFrame then
            canMove = false
        end
    elseif input.KeyCode == Enum.KeyCode.W then
        HRP.CFrame = HRP.CFrame * CFrame.new(0, 0, -10)
        if HRP.CFrame == Border1.CFrame then
            canMove = false
        end
    elseif input.KeyCode == Enum.KeyCode.S then
        if HRP.CFrame ~= Border.CFrame then
            canMove = true
            HRP.CFrame = HRP.CFrame * CFrame.new(0, 0, 10)
        end
        if HRP.CFrame == Border.CFrame then
            canMove = false
        end
    end
end)

(Yes, I have a variable for everything in the script, I just did not include them)

Thank you for your help!

-KoalaKrush

0
cant you just check if their positionj is larger than the map boundary, and if they're trying to increase it in that direction prevent the movement Wayyllon 170 — 1y
0
uhh. How would I d othat? KoalaKo_XD 27 — 1y
0
Can you add invisible walls at the edges of your map? T3_MasterGamer 2189 — 1y
0
It won't work. Because I'm moving my player with CFramne it'll go right through the wall... KoalaKo_XD 27 — 1y

Answer this question