So basically I added a bodyforce and I use this function to create the bodyforce, and set the bodyforce’s force to parameter 1 of this function:
if uis:IsKeyDown(Enum.KeyCode.W) then
dash(Vector3.new(0,0,50) * 50,rot)
end
if uis:IsKeyDown(Enum.KeyCode.S) then
dash(Vector3.new(0,0,-50),rot)
end
if uis:IsKeyDown(Enum.KeyCode.D) then
dash(Vector3.new(50,0,0),rot)
end
if uis:IsKeyDown(Enum.KeyCode.A) then
dash(Vector3.new(-50,0,0),rot)
end
But it won’t do anything at all unless I literally spam the key. My player doesn’t move forwards at all as I clearly parent the bodyforce into the root. Here is the function:
local playerDashed = false
function dash(pos,rot,lv)
if playerDashed then return end
playerDashed = true
local char = yourPlayer.Character
local root = char:FindFirstChild("HumanoidRootPart")
if root and pos then
local vel = Instance.new("BodyForce",root)
vel.Force = pos
game.Debris:AddItem(vel,10)
end
playerDashed = false
end
You are assuming that the body you are applying a force on has a mass of 1. In this case acceleration = force. Since F = ma or Force = mass x acceleration you want to set the force to the acceleration times the mass of the body. To get the mass of the body you can use this function:
local function getMass(model)
local mass = 0
for _,descendant in ipairs(model:GetDescendants()) do
if descendant:IsA("BasePart") and descendant.Massless == false then
mass = mass + descendant:GetMass()
end
end
return mass
end
local forward = (root.CFrame.LookVector * Vector3.new(1, 0, 1)).Unit
local backward = -forward
local right = (root.CFrame.RightVector * Vector3.new(1, 0, 1)).Unit
local left = -right
You can get the directions based on the root part like so.
So instead of using Vector3.new(0, 0, 50) to go forward you actually just want to do:
local FORCE = 50
local forward = (root.CFrame.LookVector * Vector3.new(1, 0, 1)).Unit
local force = mass * (forward * FORCE)
It’s back to the old problem where you have to jump now just to force yourself in the direction. Here’s the newer code.
if uis:IsKeyDown(Enum.KeyCode.A) then
dash((-yourRoot.CFrame.RightVector*Vector3.new(1,0,1).Unit),rot)
end
function dash(pos,rot,lv)
if playerDashed then return end
playerDashed = true
local function getMass(model)
local mass = 0
for _,descendant in ipairs(model:GetDescendants()) do
if descendant:IsA("BasePart") and descendant.Massless == false then
mass = mass + descendant:GetMass()
end
end
return mass
end
local char = yourPlayer.Character
local root = char:FindFirstChild("HumanoidRootPart")
if root and pos then
local vel = Instance.new("BodyForce",root)
local mass = getMass(char)
vel.Force = mass*(pos*force)
game.Debris:AddItem(vel,1)
end
playerDashed = false
end