Hi,
Currently trying to create a proper dash mechanic for a game, however I am terrible with constraints. I have set up the dash so that it works, however it likes to slightly rotate the character for a second whenever it gets enabled, which gets much more pronounced if the dash is a long one. This is especially a problem when you dash on the ground:
Since I have like, never used these before I’m not quite sure what the problem is - the creator documentation isn’t super clear about a lot of things regarding these constraints too. Here is the script that is controlling all dash logic:
--!strict
local ContextActionService = game:GetService("ContextActionService")
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
-- Since this is a StarterCharacterScript the character will always be the correct one
local character = player.Character
local rootPart = character:FindFirstChild("HumanoidRootPart")
local humanoid = character:FindFirstChildOfClass("Humanoid")
assert(rootPart:IsA("BasePart"))
assert(humanoid)
-- Create the proper LinearVelocity
local attatchment0 = Instance.new("Attachment")
local attatchment1 = attatchment0:Clone()
attatchment0.Parent = rootPart
attatchment1.Parent = rootPart
local linearVelocity = Instance.new("LinearVelocity")
linearVelocity.Attachment0 = attatchment0
linearVelocity.Attachment1 = attatchment1
linearVelocity.MaxForce = math.huge
linearVelocity.RelativeTo = Enum.ActuatorRelativeTo.World
linearVelocity.VectorVelocity = Vector3.one
linearVelocity.Enabled = false -- Set to true only when performing a move!! If set to true then the player cannot move on their own accord!
linearVelocity.Parent = rootPart
-- Function definitions for moveset
local ACTION_DASH = "Dash"
local DASH_KEYBIND = Enum.KeyCode.LeftShift
local DASH_DISTANCE = 100
local DASH_TIME = 0.1
local DASH_COOLDOWN = 0.5
local dashVelocity = DASH_DISTANCE / DASH_TIME
local dashDebounce = false
local dashReset = true
local lastDash = 0
local function dash(_, InputState: Enum.UserInputState, InputObject: InputObject)
-- is the debounce active?
-- is the UIS the correct one?
if dashDebounce
or InputState ~= Enum.UserInputState.Begin then
return
end
-- cooldown
local timeOfRequest = tick()
local delta = timeOfRequest - lastDash
if delta < DASH_COOLDOWN then
return
end
-- if in the air, only dash once
if humanoid.FloorMaterial == Enum.Material.Air then
if not dashReset then
return
end
dashReset = false
end
dashDebounce = true
lastDash = tick()
local oldVelocity = rootPart.AssemblyLinearVelocity
local dashDirection = if humanoid.MoveDirection ~= Vector3.zero then humanoid.MoveDirection else rootPart.CFrame.LookVector
dashDirection = Vector3.new(dashDirection.X, 0, dashDirection.Z).Unit
linearVelocity.VectorVelocity = dashDirection * dashVelocity
linearVelocity.Enabled = true
task.wait(DASH_TIME)
linearVelocity.Enabled = false
-- No Y velocity so things like jumps aren't maintained
rootPart.AssemblyLinearVelocity = Vector3.new(oldVelocity.X, 0, oldVelocity.Z)
dashDebounce = false
end
-- Register base moveset (may be changed by other abilities)
ContextActionService:BindAction(ACTION_DASH, dash, true, DASH_KEYBIND)
-- Connections
humanoid.StateChanged:Connect(function(_, newState: Enum.HumanoidStateType)
if newState == Enum.HumanoidStateType.Landed then
-- reset any once-in-air abilities
dashReset = true
end
end)
-- Unregister base moveset on death to not clog up ContextActionService
humanoid.Died:Connect(function()
ContextActionService:UnbindAction(ACTION_DASH)
end)