Body velocity goes slightly off course, any solutions?

Hi, I’m making a dash system. Whenever I dash backwards it goes slightly to the side at the end. It only happens when holding the s key. Any solutions? Thanks for the help.

local VECTOR_XZ: Vector3 = Vector3.new(1, 0, 1)

local RunService = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")

local character = script.Parent
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")

local function dash(_actionName, inputState)
	if inputState ~= Enum.UserInputState.Begin then
		return Enum.ContextActionResult.Pass
	end

	local connection
	local bodyVelocity = Instance.new("BodyVelocity")
	bodyVelocity.MaxForce = VECTOR_XZ * 30000
	bodyVelocity.P = math.huge
	bodyVelocity.Velocity = -humanoidRootPart.CFrame.LookVector * 40
	bodyVelocity.Parent = humanoidRootPart

	local function updateVelocity()
		bodyVelocity.Velocity = -humanoidRootPart.CFrame.LookVector * 40
	end

	connection = RunService.Heartbeat:Connect(updateVelocity)

	game.Debris:AddItem(bodyVelocity, 0.15)
	task.delay(0.15, connection.Disconnect, connection)

	return Enum.ContextActionResult.Pass
end

ContextActionService:BindAction("Dash", dash, false, Enum.KeyCode.Q)

Clip is on a new baseplate.

If I recall correctly, all the body movers are affected by mass (even if the part is set to massless, the workaround is to set custom physical properties density to .01).

So potentially it could be because of your avatar accessories, try removing those and seeing if the dash goes straight backwards?

Simple solution, don’t use BodyVelocity. It’s deprecated, as always with deprecated instances they aren’t/will not be updated and are not in sync with Roblox’s current physics environment.

Instead, use the parallel LinearVelocity, it’s the exact same but better, and it has more configurations for your scripting needs.

Thanks for the reply, I have tried LinearVelocity and I still get the same issue. I set it up very similar to the body velocity:

local bodyVelocity = Instance.new("LinearVelocity")
bodyVelocity.Attachment0 = humanoidRootPart.RootAttachment
bodyVelocity.ForceLimitMode = Enum.ForceLimitMode.PerAxis
bodyVelocity.MaxAxesForce = VECTOR_XZ * 30000
bodyVelocity.VectorVelocity = -humanoidRootPart.CFrame.LookVector.Unit * 40
bodyVelocity.Parent = humanoidRootPart

Unfortunately, changing the density for the character makes it basically unplayable. It slides a lot like it is constantly on ice and the issue still persists. Thanks for the reply though.

Comparing it to a similar script in my own game the only differences I see is how the velocity is calculated and the fact the bv is parented to the hrp after calculation

local MaxForce = Vector3.new(100000,100000,100000)
local P = 1250
local Amount = 80

if rayResult then
    if (Character.HumanoidRootPart.Position - rayResult.Position).Magnitude <= 3 then
        return
    else
		if not Character.HumanoidRootPart:FindFirstChild("BodyVelocity") then
            BodyVelocity.Parent = Character.HumanoidRootPart
            BodyVelocity.MaxForce = MaxForce
			BodyVelocity.P = P
			BodyVelocity.Velocity = Character.HumanoidRootPart.CFrame.lookVector * - Amount
            game.Debris:AddItem(BodyVelocity, .1)
        end
    end

Tried this and it didn’t fix anything. I didn’t expect it work anyways lol. Thanks for taking the time to reply and giving some of your code. I appreciate it.

You might have another script interfering, also with the mass, instead of changing it for the character, just multiply the force by root.AssemblyMass (linear velocities aren’t affected by mass anyways)

This is on a freshly made baseplate there are no script in it except the dash.

Using a linear velocity should technically fix this because it basically sets the velocity constantly with the physics system. You should try it again just using the default settings and setting the force

2 Likes

I wish. I’ve tried it, still doesn’t fix it. I can’t use default settings either because I have to change the max force. I wish I had a clue to why this happens. :sob:

Also for anyone else reading, I’ve managed to make it curve a little less using PreSimulation instead of heartbeat.

Since it uses attachments couldn’t you just change the relative to mode to Attachment0 instead of using a loop?