I am currently trying to make a simple dash script but my knowledge on how linear velocity works against body velocity is limited. Can someone explain what is wrong with the way I scripted the linear velocity?
local UserInputService = game:GetService("UserInputService")
local Character = script.Parent
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")
local Attachment = Instance.new("Attachment")
Attachment.Parent = HumanoidRootPart
local Speed = 200
local DashDuration = .2
local Cooldown = 1
local Dashing = false
local LastDash = 0
local Animator = Humanoid:FindFirstChildWhichIsA("Animator")
local Animations = {
Forwards = Instance.new("Animation");
Backwards = Instance.new("Animation");
Right = Instance.new("Animation");
Left = Instance.new("Animation");
}
Animations.Backwards.AnimationId = "rbxassetid://94244983051609"
Animations.Forwards.AnimationId = "rbxassetid://140245478716706"
Animations.Right.AnimationId = "rbxassetid://83778304105336"
Animations.Left.AnimationId = "rbxassetid://88451953083874"
local function DashAnimation(Direction)
local AnimationTrack = Animator:LoadAnimation(Animations[Direction])
AnimationTrack:Play()
AnimationTrack.Stopped:Connect(function()
AnimationTrack:Destroy()
end)
end
local function Dash()
if Dashing or tick() - LastDash < Cooldown then
return
end
Dashing = true
LastDash = tick()
local MoveDirection = Humanoid.MoveDirection
local Direction
if MoveDirection.Magnitude == 0 then
Direction = "Backwards"
elseif math.abs(MoveDirection.Z) > math.abs(MoveDirection.X) then
Direction = (MoveDirection.Z > 0) and "Forwards" or "Backwards"
else
Direction = (MoveDirection.X > 0) and "Right" or "Left"
end
DashAnimation(Direction)
local LinearVelocity = Instance.new("LinearVelocity")
LinearVelocity.MaxForce = 5000
LinearVelocity.VectorVelocity = MoveDirection.Unit * Speed
LinearVelocity.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
LinearVelocity.Attachment0 = Attachment
LinearVelocity.Parent = HumanoidRootPart
wait(DashDuration)
LinearVelocity:Destroy()
Attachment:Destroy()
Dashing = false
end
UserInputService.InputBegan:Connect(function(Input,GameProcessed)
if GameProcessed then
return
end
if Input.KeyCode == Enum.KeyCode.Q then
Dash()
end
end)
What’s wrong with it?
Also, LinearVelocity is practically the same as BodyVelocity, except with more things.
First: make sure the LinearVelocity is connected to an attachment on the HumanoidRootPart
Second: make sure the LinearVelocity.RelativeTo is set to Enum.ActuatorRelativeTo.World
Few problems. Firstly, your variables need some tuning. 200 speed is overkill. Additionally, you don’t want your relative velocity to be for attachments, as MoveDirection is in the world space. You also destroy your attachment after one dash. The last thing is that you should make sure your force limits aren’t too low. Here is the edited code I made, I also turned off force limits just for convenience.
local UserInputService = game:GetService("UserInputService")
local Character = script.Parent
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")
local Attachment = Instance.new("Attachment")
Attachment.Parent = HumanoidRootPart
local Speed = 50
local DashDuration = .5
local Cooldown = 1
local Dashing = false
local LastDash = 0
local Animator = Humanoid:FindFirstChildWhichIsA("Animator")
local Animations = {
Forwards = Instance.new("Animation");
Backwards = Instance.new("Animation");
Right = Instance.new("Animation");
Left = Instance.new("Animation");
}
Animations.Backwards.AnimationId = "rbxassetid://94244983051609"
Animations.Forwards.AnimationId = "rbxassetid://140245478716706"
Animations.Right.AnimationId = "rbxassetid://83778304105336"
Animations.Left.AnimationId = "rbxassetid://88451953083874"
local function DashAnimation(Direction)
local AnimationTrack = Animator:LoadAnimation(Animations[Direction])
AnimationTrack:Play()
AnimationTrack.Stopped:Connect(function()
AnimationTrack:Destroy()
end)
end
local function Dash()
if Dashing or tick() - LastDash < Cooldown then
return
end
Dashing = true
LastDash = tick()
local MoveDirection = Humanoid.MoveDirection
local Direction
if MoveDirection.Magnitude == 0 then
Direction = "Backwards"
elseif math.abs(MoveDirection.Z) > math.abs(MoveDirection.X) then
Direction = (MoveDirection.Z > 0) and "Forwards" or "Backwards"
else
Direction = (MoveDirection.X > 0) and "Right" or "Left"
end
DashAnimation(Direction)
local LinearVelocity = Instance.new("LinearVelocity")
LinearVelocity.ForceLimitsEnabled = false
LinearVelocity.VectorVelocity = MoveDirection.Unit * Speed
LinearVelocity.Attachment0 = Attachment
LinearVelocity.Parent = HumanoidRootPart
wait(DashDuration)
LinearVelocity:Destroy()
Dashing = false
end
UserInputService.InputBegan:Connect(function(Input,GameProcessed)
if GameProcessed then
return
end
if Input.KeyCode == Enum.KeyCode.Q then
Dash()
end
end)