Help with dash system and force

I’m trying to make a dash system based on the character’s moving direction, using VectorForce. Everything works fine at a certain point, but sometimes, usually when I jump, I get a extreme high velocity out of nowhere. I tried to use StateChanged to fix that but I’m still getting this weird result. If you realise that I did something wrong with this implementation, please let me know!

Here’s the code:

-- // Services
local UserInputService = game:GetService("UserInputService")

-- // Localization
local character = script.Parent
local root = character:WaitForChild("HumanoidRootPart")
local humanoid = character:WaitForChild("Humanoid")

-- // Constants
local DASH_NORMAL = 900 * root.AssemblyMass
local DASH_COOLDOWN = 2

-- // Booleans
local canDash = true

-- // Functions
function dash(root, humanoid)
	local mD = humanoid.MoveDirection

	local attachment = Instance.new("Attachment")
	attachment.Parent = root

	local vectorForce = Instance.new("VectorForce")
	vectorForce.Parent = root
	vectorForce.Attachment0 = attachment
	vectorForce.ApplyAtCenterOfMass = true

	humanoid.JumpPower = 0
	humanoid.WalkSpeed = 0
	
	vectorForce.Force = root.CFrame:VectorToObjectSpace(mD) * DASH_NORMAL

	coroutine.wrap(function()
		wait(.7)
		vectorForce:Destroy()
		attachment:Destroy()
		humanoid.JumpPower = 50
		humanoid.WalkSpeed = 16
		end
	end)()
end
2 Likes

Possibly due to the attachments being locked to the root part. So when you jump they also move. Try parenting the second attachment ( goal ) to a new part anchored in the workspace. This could fix the issue.

I notice that you are using vector force, and from my experience with it, vector force often does not work well for my purposes. Instead, try using body velocity, and making the velocity go in the humanoidrootpart.CFrame.LookVector, before destroying it after a few seconds to get a ‘dash’ like movement.

Hope this helps!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.