right now im making a 2d game where the player can do stuff and so on, only problem is how do i check if an image crosses the border of the frame, and better yet how can i make it so that the image cant go outside the frame
local userinput = game:GetService("UserInputService")
while task.wait() do
if userinput:IsKeyDown(Enum.KeyCode.W) or userinput:IsKeyDown(Enum.KeyCode.Up) then
script.Parent.Position = script.Parent.Position + UDim2.new(0,0,-0.008,0)
end
if userinput:IsKeyDown(Enum.KeyCode.S) or userinput:IsKeyDown(Enum.KeyCode.Down) then
script.Parent.Position = script.Parent.Position + UDim2.new(0,0,0.008,0)
end
if userinput:IsKeyDown(Enum.KeyCode.D) or userinput:IsKeyDown(Enum.KeyCode.Right) then
script.Parent.Position = script.Parent.Position + UDim2.new(0.008,0,0,0)
end
if userinput:IsKeyDown(Enum.KeyCode.A) or userinput:IsKeyDown(Enum.KeyCode.Left) then
script.Parent.Position = script.Parent.Position + UDim2.new(-0.008,0,0,0)
end
end
This function can help to check if a frame is in bounds of another frame (These don’t have to be frames; they could be any GUI element)
local function isOutOfBounds(ContainerFrame:Frame, OtherFrame:Frame)
local OtherPos, OtherSize = OtherFrame.AbsolutePosition,OtherFrame.AbsoluteSize
local ContainerPos, ContainerSize = ContainerFrame.AbsolutePosition, ContainerFrame.AbsoluteSize
if OtherPos.X < ContainerPos.X or
OtherPos.Y < ContainerPos.Y or
OtherPos.X + OtherSize.X > ContainerPos.X + ContainerSize.X or
OtherPos.Y + OtherSize.Y > ContainerPos.Y + ContainerSize.Y then
return true
end
return false
end
As for making it not go past the boundaries, I’m not super sure but you could use the function to check if the next position would make it out of bounds and then just not move the frame at all. For example, if we wanted to move to the right, but the function tells us the next position would be outside the parent frame’s bounds then we don’t move at all
i reworked the script is it something like this? but it doesnt work : (
local userinput = game:GetService("UserInputService")
local function isOutOfBounds(ContainerFrame:Frame, OtherFrame:Frame)
local OtherPos, OtherSize = OtherFrame.AbsolutePosition,OtherFrame.AbsoluteSize
local ContainerPos, ContainerSize = ContainerFrame.AbsolutePosition, ContainerFrame.AbsoluteSize
if OtherPos.X < ContainerPos.X or
OtherPos.Y < ContainerPos.Y or
OtherPos.X + OtherSize.X > ContainerPos.X + ContainerSize.X or
OtherPos.Y + OtherSize.Y > ContainerPos.Y + ContainerSize.Y then
return true
end
return false
end
while task.wait() do
local check = isOutOfBounds(script.Parent,script.Parent.Parent)
print(check)
if userinput:IsKeyDown(Enum.KeyCode.W) or userinput:IsKeyDown(Enum.KeyCode.Up) then
if check then
script.Parent.Position = script.Parent.Position + UDim2.new(0,0,-0.008,0)
end
end
if userinput:IsKeyDown(Enum.KeyCode.S) or userinput:IsKeyDown(Enum.KeyCode.Down) then
if check then
script.Parent.Position = script.Parent.Position + UDim2.new(0,0,0.008,0)
end
end
if userinput:IsKeyDown(Enum.KeyCode.D) or userinput:IsKeyDown(Enum.KeyCode.Right) then
if check then
script.Parent.Position = script.Parent.Position + UDim2.new(0.008,0,0,0)
end
end
if userinput:IsKeyDown(Enum.KeyCode.A) or userinput:IsKeyDown(Enum.KeyCode.Left) then
if check then
script.Parent.Position = script.Parent.Position + UDim2.new(-0.008,0,0,0)
end
end
end
How big is the image just set the size etc on scale then we just check if the position is > < the position within the frame by checking for -1 and then set it to -1 if it’s < -1
local userinput = game:GetService("UserInputService")
local function isOutOfBounds(ContainerFrame:Frame, OtherFrame:Frame)
local OtherPos, OtherSize = OtherFrame.AbsolutePosition,OtherFrame.AbsoluteSize
local ContainerPos, ContainerSize = ContainerFrame.AbsolutePosition, ContainerFrame.AbsoluteSize
if OtherPos.X < ContainerPos.X or
OtherPos.Y < ContainerPos.Y or
OtherPos.X + OtherSize.X > ContainerPos.X + ContainerSize.X or
OtherPos.Y + OtherSize.Y > ContainerPos.Y + ContainerSize.Y then
return true
end
return false
end
while task.wait() do
local check = isOutOfBounds(script.Parent.Parent,script.Parent)
print(check)
if userinput:IsKeyDown(Enum.KeyCode.W) or userinput:IsKeyDown(Enum.KeyCode.Up) then
if not check then
script.Parent.Position = script.Parent.Position + UDim2.new(0,0,-0.008,0)
end
end
if userinput:IsKeyDown(Enum.KeyCode.S) or userinput:IsKeyDown(Enum.KeyCode.Down) then
if not check then
script.Parent.Position = script.Parent.Position + UDim2.new(0,0,0.008,0)
end
end
if userinput:IsKeyDown(Enum.KeyCode.D) or userinput:IsKeyDown(Enum.KeyCode.Right) then
if not check then
script.Parent.Position = script.Parent.Position + UDim2.new(0.008,0,0,0)
end
end
if userinput:IsKeyDown(Enum.KeyCode.A) or userinput:IsKeyDown(Enum.KeyCode.Left) then
if not check then
script.Parent.Position = script.Parent.Position + UDim2.new(-0.008,0,0,0)
end
end
end
i tested it out and it works, but i cant move after i touch the borders, is there a way to fix this?
ive made this for a dragable map and checked when the map goes out of the border to reset the position to whats max allowed
if map.Position.Y.Scale < -1 then
map.Position = UDim2.new(map.Position.X.Scale, map.Position.X.Offset, -1, map.Position.Y.Offset)
end
if map.Position.Y.Scale > 0 then
map.Position = UDim2.new(map.Position.X.Scale, map.Position.X.Offset, 0, map.Position.Y.Offset)
end
if map.Position.X.Scale < -1 then
map.Position = UDim2.new(-1, map.Position.X.Offset, map.Position.Y.Scale, map.Position.Y.Offset)
end
if map.Position.X.Scale > 0 then
map.Position = UDim2.new(0, map.Position.X.Offset, map.Position.Y.Scale, map.Position.Y.Offset)
end
simple if the position y scale < - 1 then it sets the position in this game map map is the object you need to position to thier position valeus but y scale be set to -1 so it not goes out of the border wjere -1 is y scale - border