So I have been trying to make a custom player controller for my raycaster engine which sunply moves an anchored part around which represents the player. Currently, it works pretty well. All though running into walls is very janky and buggy looking.
Here’s what I am trying to achieve:
The green line is the part’s movement vector. When the part is brushing up against a plane/surface of one of the walls, I want the vector to change to one that is perpendicular with the plane. The vector should, however, keep all of its velocity in the plane’s direction, yet lose all velocity towards the plane.
Here’s my poor attempt at this:
local function Walk(deltaTime, Direction)
local deltaSpeed = deltaTime * PlayerWalkSpeedPerSecond
local NextPosition = (CameraPart.CFrame * CFrame.new(0, 0, -Direction * deltaSpeed)).Position
local RayDirection = CameraPart.CFrame.LookVector * (HitboxRayLength * Direction)
local WallCheckRay = workspace:Raycast(CameraPart.Position, RayDirection, CollisionParams)
if WallCheckRay and WallCheckRay.Instance.CanCollide then -- Check if we have ran into a wall
local Dot = RayDirection:Dot(WallCheckRay.Normal)
-- make the player slide along the object when collided with
NextPosition = NextPosition - (WallCheckRay.Normal * Dot * HitboxRayLength)
end
CameraPart.Position = NextPosition
end
How can I improve/fix this?