I’ve been trying to replicate the source engines movement system to make a surfing game but have been having trouble making the velocity be applied in reference to the root part and not the world.
My code:
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
end)