Custom wall collisions

I am making a custon non-physics-based character for my game that uses CFrame & Raycasting. The player works perfectly, but I have no idea on how to make wall collisions.

here’s the current movement code:

UIS = game:GetService("UserInputService")
RS = game:GetService("RunService")

local Player = nil
local rayresult
local LastCollisionPart
local LastCollisionCFrame
local MoveSpeed = 0.3

RS.Heartbeat:Connect(function()
	if game.ReplicatedStorage.CameraMode.Value ~= 0 then return end
	local MoveCFrame
	MoveCFrame = Player.CFrame.Rotation
	local FloorCollisionRayDir = Player.CFrame.UpVector * -30
	rayresult = game.Workspace:Raycast(Player.Position,FloorCollisionRayDir)
	if rayresult and LastCollisionPart == rayresult.Instance then
		if LastCollisionCFrame ~= rayresult.Instance.CFrame then
			local Rel = LastCollisionCFrame:ToObjectSpace(Player.CFrame)
			Player.CFrame = rayresult.Instance.CFrame:ToWorldSpace(Rel)
		else
			local IntersectionPoint = rayresult.Position
			Player.Position = IntersectionPoint + Player.CFrame.UpVector*Player.Size.Y/2
		end
	end
	if UIS:IsKeyDown(Enum.KeyCode.LeftShift) then
		MoveSpeed = 0.5
	else
		MoveSpeed = 0.3
	end
	local Grounded = not not rayresult -- Weird lua stuff ;)
	if UIS:IsKeyDown(Enum.KeyCode.W) and Grounded then
		Player.CFrame = Player.CFrame + MoveCFrame.LookVector * MoveSpeed
	end
	if UIS:IsKeyDown(Enum.KeyCode.S) and Grounded then
		Player.CFrame = Player.CFrame + MoveCFrame.LookVector * -MoveSpeed
	end
	if UIS:IsKeyDown(Enum.KeyCode.D) and Grounded then
		Player.CFrame = Player.CFrame + MoveCFrame.RightVector * MoveSpeed
	end
	if UIS:IsKeyDown(Enum.KeyCode.A) and Grounded then
		Player.CFrame = Player.CFrame + MoveCFrame.RightVector * -MoveSpeed
	end
	rayresult = game.Workspace:Raycast(Player.Position,FloorCollisionRayDir)
	if rayresult then
		LastCollisionPart = rayresult.Instance
		LastCollisionCFrame = rayresult.Instance.CFrame
	else

	end
end)

Any suggestions?

1 Like