Linear velocity strafing problem

When I strafe back and forth, for some reason the linear velocity or the humanoid look vector twitches when switching direction. It’s not meant to do that, I want it to just move back and forth in a straight line.

Heres whats happening:

Code:

local function WalkInput()
	if InputHandler.W_Held then
		return 1
	elseif InputHandler.S_Held then
		return -1
	end
	return 0
end

RunService.Heartbeat:Connect(function()
	local WalkDirection = WalkInput()
	ControllerManager.FacingDirection = HumanoidRootPart.CFrame.LookVector
	
	local HumanoidForward = HumanoidRootPart.CFrame.LookVector * WalkDirection
	game:GetService("TestService"):Message(HumanoidForward)
	
	local HumanoidVector2Forward = Vector2.new(HumanoidForward.X, HumanoidForward.Z)
	local WalkVeloc = HumanoidVector2Forward * (40)
	
	PlayerVelocity.PlaneVelocity = Vector2.new(WalkVeloc.X, WalkVeloc.Y) --* Modifier
end)

So far I’ve tried searching up a solution regarding the rotation but it doesn’t seem to work

Is PlayerVelocity a LinearVelocity / BodyVelocity object? I’ve encountered a similar problem before where whenever the target force is diagonal (i.e. not aligned with the world axis) they have this weird momentum issue when you change directions. I found this post which has videos of this issue and the movement looks to be the same as in your video.

I resolved the issue in my case by ditching the LinearVelocity object and instead using BasePart:ApplyImpulse() every heartbeat, maybe you could try a similar approach?

1 Like

PlayerVelocity is a LinearVelocity in which it is set as a plane velocity. After watching the video and reviewing the movement system again in diagonals and straight lines, yeah it does seem similar to the problem another thing I’ve noticed after close observation is how the twitching is strengthened the more you are in a diagonal.

I would try using ApplyImpulse, though if it ever didn’t fit my needs are there suggestions for any alternatives?

1 Like

You could maybe try a hacky approach with AlignPosition or LineForce but I don’t know they might have the same diagonal bias so take that with a grain of salt. I think ApplyImpulse every heartbeat should work fine as that’s basically what BodyMovers do but with preset rules (and those rules are not uniform in all directions, thanks roblox!) Outside of that I don’t have much for ideas.

1 Like

Sure, I guess I’ll try the other mover constraints. I liked the tangent axis of LinearVelocity as it allowed me to stick the characters to a slope. I’ll comment on this post to see what works best for a custom movement system or at least my situation if others have similar issues like this.

Thanks for the help!

1 Like

Instead of using :ApplyImpulse I used VectorForce instead, I think it should be similar enough to :ApplyImpulse as I based it off the documentation for ‘AssemblyLinearVelocity’

And then apply drag to it that way the force is equalized once you reach a speed threshold

The code I’m currently using:

RunService.Heartbeat:Connect(function()
        ControllerManager.FacingDirection = HumanoidRootPart.CFrame.LookVector.Unit
	local RootAssembly = HumanoidRootPart.AssemblyLinearVelocity

	local HumVeloc = HumanoidRootPart.AssemblyLinearVelocity
	local Impulse =	(Humanoid.MoveDirection * desiredspeed^2) * rate
	
	PlayerVelocity.Force = Impulse
	
	local velocity = Vector3.new(HumanoidRootPart.AssemblyLinearVelocity.X, 0, HumanoidRootPart.AssemblyLinearVelocity.Z)
	local speed = velocity.Magnitude
	
	if speed > 0.1 then
		Drag.Force = (-velocity.Unit*((speed)^2)*XZVector) * rate
	else
		Drag.Force = Vector3.new()
	end
end)

It has 2 ‘VectorForces’ 1 controls movement and the other controls drag.
‘Rate’ makes it so you can adjust how snappy or slippery it is.
I still haven’t written a code for adjusting the velocity based on slopes and material friction. However, this should be a good basis for anyone looking for an alternative to linear velocity.