Getting the distance between the player's feet and a small step (Custom character controler)

I’m making a capsule based character controler in roblox, I’ve got the base movement working like jumping and walking and now I want to add functionality for stairs, so that the collider doesn’t get stuck on a step, I know a way to fix this issue, once the player gets near a step just teleport them upwoards based on the distance between the players feet and the top of the step, but thats the thing, how do i get that??

Here’s the collider and it’s collision detectors look like:


The ground detection collision is just for jumping, it does nothing here.

So basically whenever the blue collider detects a wall and if that wall does not collide with the wall collider then the controler considers that as a step, therefore it should teleport the player upwoards to compensate. Heres the code that detects that:

function checkForStep()
	local stepParts = workspace:GetPartsInPart(collider:FindFirstChild("StepCheck"), ovParams)
	local wallParts = workspace:GetPartsInPart(collider:FindFirstChild("WallCheck"), ovParams)
	local step = false
	for _, p in pairs(stepParts) do
		if not table.find(wallParts, p) then
			step = true
		end
	end 
	if step then
		print("step")
		local distFromFeet = (collision.Position - collision.Size.X/2) -- THIS IS WHERE I AM HAVING PROBLEMS, ITS NOT FINISHED
		velocity = Vector3.new(0,200,0)
		collision:ApplyImpulse(velocity*100*globalDelta)
	end
end

(The checkForStep function gets called every frame using RunService.RenderedStep)

In summary, just do this…
image

Best practice would be to stop using “GetPartsInPart” and convert it to use Raycasting instead.

I’m not entirely sure if it’s even plausible to get the distance from the upward facing normal and your foot without using rays. Each part has a different orientation, therefor a different up-vector / face normal, so you wouldn’t be able to determine the position of top of the part, etc… with using GetPartsInParts.

I am using GetPartsInPart over Raycasting because its more accurate and easier to code, but yeah it does have that limitation, but how could I implement raycasting into this? Shooting rays from the feet all the way up the collider to see where they hit?
image

I can’t imagine that GetPartsInPart is more accurate, but that’s besides the point. I’ve worked on a few character controllers in the past, and what I tend to do is first make an arbitrary value, which is the step height. This is the distance from the ground a player should be able to “step” up.

From there, you can raycast in a circle, at the bottom point of the controller + step height, a graph of what this might look like:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.