Assistance making a custom character controller

I am trying to make a character controller that emulates the source engine controller. However the humanoid applies forces even when disabling the ControlModule. I’ve tried to solve this using the Physics state of Humanoid however the humanoid will not retain its upright position in this state. I tried to solve this by setting the players CFrame every rendered frame however it doesn’t seem to work.

My code as of now:

local RepStore = game.ReplicatedStorage
local RunService = game:GetService("RunService")
local InputService = game:GetService("UserInputService")

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local rootpart = character:WaitForChild("HumanoidRootPart")
local camera = workspace.CurrentCamera

local MAX_SPEED = 50
local MAX_ACC = 10 * MAX_SPEED

-- Initialize Character
local force = Instance.new("BodyForce")
force.Name = "Force"
force.Parent = rootpart

local inputKeys = {
    [Enum.KeyCode.W] = Vector3.new(0,0,1),
    [Enum.KeyCode.A] =  Vector3.new(-1,0,0),
    [Enum.KeyCode.S] = Vector3.new(0,0,-1),
    [Enum.KeyCode.D] = Vector3.new(1,0,0)
}

function getwishdir()
    local wishdir = Vector3.new()
    for key, dir in pairs(inputKeys) do
        if InputService:IsKeyDown(key) then
            wishdir += dir
        end
    end
    return wishdir
end

function update_vel(wishdir, vel, frame_time)
    local current_speed = vel:Dot(wishdir)
    local add_speed = math.clamp(MAX_SPEED - current_speed, 0, MAX_ACC * frame_time)
    
    return vel + add_speed * wishdir
end

RunService.RenderStepped:Connect(function(deltaTime)
    local vel = rootpart.Velocity
    local newVel = update_vel(getwishdir(), vel, deltaTime)
    rootpart.Velocity = newVel
    rootpart.CFrame = CFrame.lookAt(rootpart.Position, Vector3.new(camera.CFrame.ZVector.X,0,camera.CFrame.ZVector.Z))
end)
1 Like