Slope Custom Movement

  1. What do you want to achieve? I would like my character to be able to move on inclined floors

  2. What is the issue? Watch the videos for yourself and you will see the problem

function ControlModule:OnSlope()
	local rootPart = self.Client:GetCharacter():GetRootPart()
	local rootPartPosition = rootPart.Position

	local groundCast = VisualCast.new(Color3.new(0,1,0), workspace, true)
	local groundHit = groundCast :Raycast(rootPartPosition , -Vector3.yAxis, Config.HipHeight + 1, self.RaycastParams:GetRaycastParams())

	self.GroundHitNormal = groundHit and groundHit .Normal

	if groundHit then
		local upVector = Vector3.yAxis
		local dotProduct = upVector:Dot(self.GroundHitNormal)
		local angle = math.deg(math.acos(dotProduct))

		return true
	end

	return false
end

function ControlModule:GetSlopeMoveDirection(moveDirection, slopeHitNormal)
    local dotProduct = moveDirection:Dot(slopeHitNormal)
    local projection = moveDirection - slopeHitNormal * dotProduct

    local normalizedProjection = projection.Unit

    return normalizedProjection
end

function ControlModule:OnRenderStepped(dt :number)
	local character = self.Client:GetCharacter()
	local worldCharacter = character:GetWorldCharacter()

	local activeController = self:GetActiveController()
	self.MoveDirection = activeController and activeController :GetMoveVector()

	local moveVector = self.MoveDirection

	local isJumping = character:IsJumping()

	if self:OnSlope() and not isJumping and moveVector ~= VECTOR3_ZERO then
		moveVector = self:GetSlopeMoveDirection(moveVector, self.GroundHitNormal)
	end

	self.MoveVectorSpeed = moveVector * character:GetSpeed()
	worldCharacter :PivotTo(worldCharacter:GetPivot() * CFrame.new(self.MoveVectorSpeed * dt) * CFrame.new(self.Velocity * dt))
end



1 Like