How i do fix this buggy 2D Collision?`
Code (Client):
GameRender = game:GetService("RunService")
UIS = game:GetService("UserInputService")
LPlayer = game:GetService("Players").LocalPlayer
Camera = workspace.CurrentCamera
Char = LPlayer.Character or LPlayer.CharacterAdded:Wait()
PlayerHitbox =script.Parent.Player
GameFrames = script.Parent.Interacts
Humanoid = Char:WaitForChild("Humanoid")
local left,right,IsJumping,YBasedVelocity,runspeed = false,false,false,0,1 -- 1 is normal speed
local Collision
local WallCollison
Humanoid.WalkSpeed = 0
script.Parent.BGMusic:Play()
Camera.CameraType = Enum.CameraType.Scriptable
Camera.CFrame = workspace.BaseTarget.CFrame
Frameworks = {
["walk"] = {
Vector2.new(0,2),
Vector2.new(20,2),
Vector2.new(0,2)
}
}
function GoLeft()
Collision = false
local nowobjects = GameFrames:GetChildren()
for index,object in pairs(nowobjects) do
if
object.AbsolutePosition.X + object.AbsoluteSize.X > PlayerHitbox.AbsolutePosition.X and
object.AbsolutePosition.X + object.AbsoluteSize.X< PlayerHitbox.AbsolutePosition.X + (PlayerHitbox.AbsoluteSize.X/2) and
object.AbsolutePosition.Y < PlayerHitbox.AbsolutePosition.Y + PlayerHitbox.AbsoluteSize.Y and
object.AbsolutePosition.Y + object.AbsoluteSize.Y > PlayerHitbox.AbsolutePosition.Y
then
Collision = true
end
if not Collision then
PlayerHitbox.Sprite.ImageRectSize = Vector2.new(-20,20)
--PlayerHitbox.Sprite.ImageRectOffset = Vector2.new(20,2)
PlayerHitbox.Position = PlayerHitbox.Position - UDim2.new(.001*runspeed,0,0,0)
end
end
end
function GoRight()
Collision = false
local nowobjects = GameFrames:GetChildren()
for index,object in pairs(nowobjects) do
if
object.AbsolutePosition.X < PlayerHitbox.AbsolutePosition.X + PlayerHitbox.AbsoluteSize.X and
object.AbsolutePosition.X > PlayerHitbox.AbsolutePosition.X + (PlayerHitbox.AbsoluteSize.X/2) and
object.AbsolutePosition.Y < PlayerHitbox.AbsolutePosition.Y + PlayerHitbox.AbsoluteSize.Y and
object.AbsolutePosition.Y + object.AbsoluteSize.Y > PlayerHitbox.AbsolutePosition.Y
then
Collision = true
end
if not Collision then
PlayerHitbox.Sprite.ImageRectSize = Vector2.new(20,20)
PlayerHitbox.Position = PlayerHitbox.Position + UDim2.new(.001*runspeed,0,0,0)
end
end
end
function JumpPlayer()
IsJumping =true
YBasedVelocity = .03
if PlayerHitbox.Sprite.ImageRectSize == Vector2.new(-20,20) then
PlayerHitbox.Sprite.ImageRectOffset = Vector2.new(60,2)
end
if PlayerHitbox.Sprite.ImageRectSize == Vector2.new(20,20) then
PlayerHitbox.Sprite.ImageRectOffset = Vector2.new(40,2)
end
script.Parent.Jump:Play()
print("do")
end
function GroundCheck()
local nowobjects = GameFrames:GetChildren()
for index,object in pairs(nowobjects) do
if object.AbsolutePosition.X < PlayerHitbox.AbsolutePosition.X + PlayerHitbox.AbsolutePosition.X and object.AbsolutePosition.X + object.AbsolutePosition.X > PlayerHitbox.AbsolutePosition.X and object.AbsolutePosition.Y < PlayerHitbox.AbsolutePosition.Y + PlayerHitbox.AbsolutePosition.Y and object.AbsolutePosition.Y > PlayerHitbox.AbsolutePosition.Y - (PlayerHitbox.AbsolutePosition.Y/2) then
return {true,object.Position.Y.Scale}
end
end
return {false, 0}
end
function BumpFunction()
local nowobjects = GameFrames:GetChildren()
for index,object in pairs(nowobjects) do
if
object.AbsolutePosition.X < PlayerHitbox.AbsolutePosition.X + PlayerHitbox.AbsoluteSize.X and
object.AbsolutePosition.X + object.AbsoluteSize.X > PlayerHitbox.AbsolutePosition.X and
object.AbsolutePosition.Y < PlayerHitbox.AbsolutePosition.Y + PlayerHitbox.AbsoluteSize.Y and
object.AbsolutePosition.Y > PlayerHitbox.AbsolutePosition.Y - (PlayerHitbox.AbsoluteSize.Y/2)
then
-- WallCollison = true
-- WallCollisonY = object.Position.Y.Scale
return {true,object.Position.Y.Scale+object.Size.Y.Scale}
end
end
return {false, 0}
end
function Rendergame()
local GC = GroundCheck()
local BumpSettings = BumpFunction()
onground = GC[1]
groundy = GC[2]
if left then
GoLeft()
end
if right then
GoRight()
end
if IsJumping and GroundCheck() then
YBasedVelocity = YBasedVelocity - .002
spawn(function()
if YBasedVelocity < 0.013 then
if PlayerHitbox.Sprite.ImageRectSize == Vector2.new(-20,20) then
PlayerHitbox.Sprite.ImageRectOffset = Vector2.new(80,2)
end
if PlayerHitbox.Sprite.ImageRectSize == Vector2.new(20,20) then
PlayerHitbox.Sprite.ImageRectOffset = Vector2.new(60,2)
end
end
end)
if YBasedVelocity < -0.01 then
YBasedVelocity = 0
IsJumping = false
if PlayerHitbox.Sprite.ImageRectSize == Vector2.new(-20,20) then
PlayerHitbox.Sprite.ImageRectOffset = Vector2.new(80,2)
end
if PlayerHitbox.Sprite.ImageRectSize == Vector2.new(20,20) then
PlayerHitbox.Sprite.ImageRectOffset = Vector2.new(60,2)
end
end
end
if not IsJumping then
if onground then
YBasedVelocity = 0
if PlayerHitbox.Sprite.ImageRectSize ~= Vector2.new(-20,20) then
PlayerHitbox.Sprite.ImageRectOffset = Vector2.new(0,2)
end
if PlayerHitbox.Sprite.ImageRectSize ~= Vector2.new(20,20) then
PlayerHitbox.Sprite.ImageRectOffset = Vector2.new(20,2)
end
PlayerHitbox.Position = UDim2.new(PlayerHitbox.Position.X.Scale,0,groundy-PlayerHitbox.Size.Y.Scale,0)
elseif YBasedVelocity > -.01 then
YBasedVelocity = YBasedVelocity -.002
end
end
-- if PlayerHitbox.Sprite.ImageRectSize == Vector2.new(-20,20) then
-- PlayerHitbox.Sprite.ImageRectOffset = Vector2.new(80,2)
-- end
-- if PlayerHitbox.Sprite.ImageRectSize == Vector2.new(20,20) then
-- PlayerHitbox.Sprite.ImageRectOffset = Vector2.new(60,2)
-- end
PlayerHitbox.Position = PlayerHitbox.Position - UDim2.new(0,0,YBasedVelocity,0)
end
UIS.InputBegan:Connect(function(Input,Gpe)
if Input.KeyCode == Enum.KeyCode.Left then
left = true
elseif Input.KeyCode == Enum.KeyCode.Right then
right = true
elseif Input.KeyCode == Enum.KeyCode.Z and onground then
JumpPlayer()
print("jump")
elseif Input.KeyCode == Enum.KeyCode.X then
runspeed = 4
end
end)
UIS.InputEnded:Connect(function(Input,Gpe)
if Input.KeyCode == Enum.KeyCode.Left then
left = false
elseif Input.KeyCode == Enum.KeyCode.Right then
right = false
elseif Input.KeyCode == Enum.KeyCode.X then
runspeed = 1
end
end)
GameRender:BindToRenderStep("Camera",Enum.RenderPriority.Camera.Value + 1,Rendergame)
Video
https://www.youtube.com/watch?v=78uwJmdG2eA&feature=youtu.be
Thx for your answers!