Trouble with a LinearVelocity dash system

I’ve tried many iterations of other peoples directional movement / dodgeroll systems using specifically LinearVelocity (since BodyVelocity is deprecated). However I cannot get the movement direction consistent. “Forwards” & “Backwards” are near perfectly consistent but both sideways directions seem to get confused. Script below.

Alternatively, if there is a better way to create a LinearVelocity dash I would love some advice ^^

function combatController:dodgeRoll(player, character)
	
	local hum = character.Humanoid
	local hrp = character.HumanoidRootPart
	local root = hrp.RootAttachment
	hum.WalkSpeed = 0
	
	local newVel = Instance.new("LinearVelocity")
	newVel.Parent = hrp
	newVel.Attachment0 = root
	newVel.MaxForce = math.huge
	
	local direction = tostring(math.round(hum.MoveDirection:Dot(hrp.CFrame.LookVector)))
	print(direction)
	-- I really hate using tostring here but apparently 0 == -0 >:(
	
	if direction == "1" then
		direction = "forward"
	elseif direction == "-1" then
		direction = "backwards"
	elseif direction == "0" then
		direction = "left"
	elseif direction == "-0" then
		direction = "right"
	end
	
	print(direction)
	
	local velocity = 50

	if direction == "forward" then
		newVel.VectorVelocity = hrp.CFrame.LookVector * velocity
	elseif direction == "backwards" then
		newVel.VectorVelocity = hrp.CFrame.LookVector * -velocity
	elseif direction == "left" then
		newVel.VectorVelocity = hrp.CFrame.RightVector * -velocity
	elseif direction == "right" then
		newVel.VectorVelocity = hrp.CFrame.RightVector * velocity
	end
	
	task.wait(.1)
	newVel:Destroy()
	hum.WalkSpeed = 16
end

Why did you made it a method if you never use self?
You are looking for https://create.roblox.com/docs/reference/engine/classes/BasePart#ApplyImpulse likely.

Thank you, I’ll have a look at ApplyImpulse.

Another thing, is it bad practice to be using methods if there’s no need for self? I’m still a bit confused module scripts. : p

Update, this didn’t work.

Basically just works the same as my previous script, still makes left go right & right go left about 25% of the time.

its inconsistent because dot product with hrp look vector does not give you enough information to determine which side you wanna dash. utilize right vector for that and avoid tostring with math operations, that’s a really hacky approach u got there.
try something like this

local moveDir = hum.MoveDirection
local lookScalar, rightScalar = moveDir:Dot(hrp.CFrame.LookVector), moveDir:Dot(hrp.CFrame.RightVector)
local direction = 'forward'
if lookScalar < -0.5 then
	direction = 'backwards'
elseif rightScalar > 0.5 then
	direction = 'right'
elseif rightScalar < -0.5 then
	direction = 'left'
end
1 Like

That works perfectly - Thank you! : )

Could I ask which category this kind of scripting falls into? Psychics(?) is one of my weak points and I’d like to improve on this.

1 Like

this is vector math. here’s a video that helped me a lot. its a bit long but totally worth it

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