Platformer Movement Issue (raycasting)

Howdy,

I’m working on a platformer combat game and I’m having an issue with players jumping into elevated pads. Sometimes, when a player jumps into a pad, my collision script doesn’t detect the platform and will get the player stuck and fling them. I have no problem with the flinging issue, my main problem is the collision detection with the raycast.

Video:

Source Code: this is run on the client, obviously. the collidable parts are stored in a folder in workspace and are cancollide = false by default.

function replicator:FloorCheck(obj)
	local r = Ray.new(obj.Position+Vector3.new(0,-2.2,0),Vector3.new(0,-1e2,0))
	local p,pos = workspace:FindPartOnRayWithWhitelist(r,{workspace.Collidable})
	if p and p.Name:match('Platform') then
		p.CanCollide = true
	else
		for _,v in ipairs(workspace.Collidable:GetChildren()) do
			if v.Name:match('Platform') then
				v.CanCollide = false
			end
		end
	end
	local c = ASSETS.DebugPart:Clone() --debug part of code, shown with the white dot under the player
	c.Parent = workspace.LiveEffectsasdf
	c.Position = pos
	Debris:AddItem(c,0.01)
end

edit: the function is run every .Stepped.

1 Like

You could make it not detect floor detection when the humanRootPart.Velocity.Y > 0. That would stop it from detecting it while jumping upwards.

Just did this:
https://gyazo.com/d8e9afb24c24d57b4ec1a549d3fd36a6
And with higher jumppower:
https://gyazo.com/c17493c8c5532d0d377d2df97b42dda8

(sick looking game btw)

1 Like

I am not really sure what you mean but I am assuming that you are having problems with the can collide turning back to true when the player is going through the block?

If that is so then

You could cast a ray from the player’s foot and if it detects it then change the collision

game:GetService('RunService').Stepped:Connect(function()
	local ray = Ray.new(game.Players.LocalPlayer.Character.HumanoidRootPart.Position, Vector3.new(0, 30, 0))
	local ray2 = Ray.new(game.Players.LocalPlayer.Character.RightFoot.Position, Vector3.new(0, -30, 0))
	local hit, pos = workspace:FindPartOnRay(ray, game.Players.LocalPlayer.Character)
	local hit2, pos2 = workspace:FindPartOnRay(ray2, game.Players.LocalPlayer.Character)
	if hit and hit.Name == 'Cloud' then
		hit.CanCollide = false
	end
	if hit2 and hit2.Name == 'Cloud' then
		hit2.CanCollide = true
	end
end)

This is what i did for something like that

1 Like

Ding ding ding!

This is a lot more efficient and accurate than me having to raycast every frame. Thanks a lot! :smiley:

1 Like