hheeeey guys its my first post here or topic
so im making a 2d gam- experience and the collisions is not accurate
what? roblox is a place to make 3d stuff? uhhhhhh no i dont think so
as you can see, its clipping through objects
uh yeah heres the code
(prepare your eyes for the worst stuff ever made)
local Players = game:GetService("Players")
local StarterGui = game:GetService("StarterGui")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local GuiCollisionService = require(ReplicatedStorage.GuiCollisionService)
local Background = script.Parent.Parent.Background
local Objects = Background.Objects
local Player = Background.Player
local isMoving = false
local isFalling = false
local Direction = nil
local CollideTop = false
local CollideLeft = false
local CollideRight = false
local CollideBottom = true
local canmoveright = true
local canmoveleft = true
local speed = 5 -- Larger = Faster, Smaller = Slower
local jump = 15
local gravityvelocity = 0
local fallvelocitycap = 50
local leftvelocity = 0
local rightvelocity = 0
local forcedleftvelocity = 0
local forcedrightvelocity = 0
local objectscollidingleft = {}
local objectscollidingright = {}
local objectscollidingtop = {}
local objectscollidingbottom = {}
local function Fall()
isFalling = true
if fallvelocitycap > gravityvelocity then
gravityvelocity += 1
end
end
jump = jump * -1
local totalTime = 0
RunService.Stepped:Connect(function(dtime)
totalTime += dtime
if (totalTime < 1 / 60) then
return
end
totalTime = 0
for i, v in pairs(Objects:GetDescendants()) do
if v:FindFirstAncestorWhichIsA("Frame") == nil and v:FindFirstAncestorWhichIsA("ImageLabel") == nil then
if v:IsA("Frame") or v:IsA("ImageLabel") then
if GuiCollisionService.isColliding(Player.Collision.Bottom, v) == true then
if table.find(objectscollidingbottom, v) == nil then
table.insert(objectscollidingbottom, v)
end
else
if table.find(objectscollidingbottom, v) ~= nil then
table.remove(objectscollidingbottom, table.find(objectscollidingbottom, v))
end
end
if GuiCollisionService.isColliding(Player.Collision.Top, v) == true then
if table.find(objectscollidingtop, v) == nil then
table.insert(objectscollidingtop, v)
end
else
if table.find(objectscollidingtop, v) ~= nil then
table.remove(objectscollidingtop, table.find(objectscollidingtop, v))
end
end
if GuiCollisionService.isColliding(Player.Collision.Left, v) == true then
if table.find(objectscollidingleft, v) == nil then
table.insert(objectscollidingleft, v)
end
else
if table.find(objectscollidingleft, v) ~= nil then
table.remove(objectscollidingleft, table.find(objectscollidingleft, v))
end
end
if GuiCollisionService.isColliding(Player.Collision.Right, v) == true then
if table.find(objectscollidingright, v) == nil then
table.insert(objectscollidingright, v)
end
else
if table.find(objectscollidingright, v) ~= nil then
table.remove(objectscollidingright, table.find(objectscollidingright, v))
end
end
end
-----------------------------------------------------------------------------------------
if v.Parent:IsA("CanvasGroup") == false then
if v:IsA("ImageLabel") or v:IsA("Frame") or v:IsA("CanvasGroup") then
v.Position = UDim2.fromOffset(v.Position.X.Offset - forcedrightvelocity, v.Position.Y.Offset)
if canmoveright == true and CollideRight == false then
v.Position = UDim2.fromOffset(v.Position.X.Offset - rightvelocity, v.Position.Y.Offset)
end
v.Position = UDim2.fromOffset(v.Position.X.Offset + forcedleftvelocity, v.Position.Y.Offset)
if canmoveleft == true and CollideLeft == false then
v.Position = UDim2.fromOffset(v.Position.X.Offset + leftvelocity, v.Position.Y.Offset)
end
v.Position = UDim2.fromOffset(v.Position.X.Offset, v.Position.Y.Offset - gravityvelocity)
end
end
end
end
if #objectscollidingleft >= 1 then
CollideLeft = true
else
CollideLeft = false
end
if #objectscollidingright >= 1 then
CollideRight = true
else
CollideRight = false
end
if #objectscollidingtop >= 1 then
CollideTop = true
gravityvelocity = 0
else
CollideTop = false
end
if #objectscollidingbottom >= 1 then
CollideBottom = true
isFalling = false
gravityvelocity = 0
else
CollideBottom = false
Fall()
end
-----------------------------------------
if forcedrightvelocity > 0 then
canmoveleft = false
forcedrightvelocity -= 1
else
canmoveleft = true
end
if forcedleftvelocity > 0 then
canmoveright = false
forcedleftvelocity -= 1
else
canmoveright = true
end
if UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) == true then
speed = 10
else
speed = 5
end
if CollideRight == true then
forcedrightvelocity = 0
end
if CollideLeft == true then
forcedleftvelocity = 0
end
if UserInputService:IsKeyDown(Enum.KeyCode.Right) == true and CollideRight == false then
leftvelocity = 0
rightvelocity = speed
elseif UserInputService:IsKeyDown(Enum.KeyCode.Left) == true and CollideLeft == false then
rightvelocity = 0
leftvelocity = speed
else
rightvelocity = 0
leftvelocity = 0
end
if UserInputService:IsKeyDown(Enum.KeyCode.Z) == true then
if isFalling == false then
gravityvelocity = jump
elseif isFalling == true then
if CollideRight == true then
gravityvelocity = jump + 5
forcedleftvelocity = 15
end
if CollideLeft == true then
gravityvelocity = jump + 5
forcedrightvelocity = 15
end
end
end
end)
wasnt that horrible?
also pleas dont be rude, im new here and my coding sucks :c