Custom Character clipping through tilted parts

Hi, I’m coding a “Kinematic Character Controller” on roblox, completely ditching Humanoids. but i’ve ran into an issue that i can’t seem to fix.

The character completely clips through the tilted part (represents the roof)
What i want to achieve is the character just stops when they run into a space that’s too small to fit through. (Similar to a humanoid behavior)

This is the piece of code that handles the collision:
ProjectVectorOnPlane formula:

local function ProjectVectorOnPlane(vectorToProject : Vector3, planeNormal : Vector3): Vector3
	local dotProduct = vectorToProject:Dot(planeNormal)
	
	return vectorToProject - planeNormal * dotProduct
end

Projecting along a plane and sliding the character with it (Ground and Roof uses the same code except the CastDirection is inverted)

SavedData.GetGround = function(Position: Vector3): RaycastResult
	local CastDirection = Vector3.new(0, -3, 0)
	
	local Cast = workspace:Blockcast(CFrame.new(Position), Vector3.new(2, 0.125, 2), CastDirection, NCPARAMS)
	
	if Cast then
		SavedData.TargetVelocity = VEssentials.ProjectVectorOnPlane(SavedData.TargetVelocity, Cast.Normal)
	end
	
	SavedData.GroundRay = Cast
	
	return Cast
end

And the character is being controlled with:

Root:PivotTo(CFrame.new(GroundPosition) * CFrame.lookAlong(StateData.TargetVelocity, VEssentials.GetHorizontalProjection(workspace.CurrentCamera.CFrame.LookVector)))

GetHorizontalProjection() is Vector3.new(X.X, 0, X.Z)

The top & bottom debug part represents the roof cast’s hit position (green if hit and yellow if nil)
The red and blue is the collision check for rays to go depending on the player’s Velocity

Any possible help is greatly appreciated,
Cheers!

Try running the detection all the time. One way you could do this is store the direction in some older variable from which you will raycast for collission. You can set the variable in a way so that if the movement is zero then it won’t set that specific variable but if movement is not zero then it would set the variable. I used this same tactic to fix the same issue in my movement system.

1 Like

I run the collision check on RenderStepped, By “setting a variable when movement isn’t zero”. I’ve tried that method, but it just snapped the character to a random position when it should be setting it between the roof hit and ground hit. I could be wrong, Could you show an example code used in your movement system that worked?

Cheers!

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