Im making a 2D GUI game for learning purposes, do you know how I could make obstacles?
I’m using UDim2 for Movement.
-- || Options ||
Test = true -- Debug stuff
local gui = game.Players.LocalPlayer.PlayerGui.Screen
if Test == true then
gui.Player.dbug.Visible=true
end
local m = game.Players.LocalPlayer:GetMouse()
local debounce = false
m.KeyDown:connect(function(k)
if k == "d" then
debounce=false
while true do
wait()
gui.Player.Position += UDim2.new(0.01,0)
gui.Player.dbug.Text = "left"
if debounce == true then break end
end
end
if k == "a" then
debounce=false
while true do
wait()
gui.Player.Position += UDim2.new(-0.01,0)
gui.Player.dbug.Text = "right"
if debounce == true then break end
end
end
if k == "w" then
debounce=false
while true do
wait()
gui.Player.Position += UDim2.new(0,0,-0.01,0)
gui.Player.dbug.Text = "up"
if debounce == true then break end
end
end
if k == "s" then
debounce=false
while true do
wait()
gui.Player.Position += UDim2.new(0,0,0.01,0)
gui.Player.dbug.Text = "down"
if debounce == true then break end
end
end
end)
m.KeyUp:connect(function(k)
debounce=true
end)
local SHOW_HITBOXES = true
local function DrawHitbox(guiElement)
if not guiElement:IsA("GuiBase2d") then return end
local gui = guiElement:FindFirstAncestorWhichIsA("LayerCollector") -- get the gui container
local absPos = guiElement.AbsolutePosition
local absSize = guiElement.AbsoluteSize
local absRot = guiElement.AbsoluteRotation
local hitbox = Instance.new("Frame")
hitbox.BackgroundTransparency = 1 -- make it invisible
hitbox.Position = UDim2.fromOffset(absPos.X, absPos.Y)
hitbox.Size = UDim2.fromOffset(absSize.X, absSize.Y)
hitbox.Rotation = absRot
if SHOW_HITBOXES then
local outline = Instance.new("UIStroke")
outline.Color = Color3.new(1,0,0) -- set outline color to red
outline.Parent = hitbox
end
hitbox.Parent = gui
end
This is just a simple function for drawing and visualizing a hitbox. It will not update or “follow” the element it surrounds. It is just an example. If you want to detect 2d collisions with hitboxes then you already have all the information you need with the aforementioned AbsolutePosition, AbsoluteSize, and AbsoluteRotation properties.
If you are having trouble understanding how to implement this then I recommend checking out this tutorial on the Separating Axis Theorem.
Sorry I was a bit vague, I want to know how I would make it impossible to walk through walls.
The code provided on the post is movement, so if A is pressed Udim2.new blah blah blah.
[its a 2d gui game]