Walkspeed Issue with Movement System

Hello Everyone, so I was making my Movement System, and I found a really incomprehensible bug with WalkSpeed. So I’ve made a thing, that changes WalkSpeed depending on which way the player goes. For example if player goes sideways or backwards, then WalkSpeed would be 16, if forward, then WalkSpeed smoothly increasing to 24.

But if player goes straight to -180 orientation by Y, the WalkSpeed would be abruptly changing to 21-22 when going to forward, not sideways, etc.

I don’t really know why it’s happening.
Here’s the part of code, which is responsible for that:

task.spawn(function()
		while task.wait(0.01) do
			local targetSpeed = initialSpeed
			local moveDirection = self.Humanoid.MoveDirection
			local facingDirection, rightDirection = 
				self.HumanoidRootPart.CFrame.LookVector, self.HumanoidRootPart.CFrame.RightVector
			if moveDirection.Magnitude > 0.1 then
				local forwardDotProduct, sidewaysDotProduct = 
					moveDirection:Dot(facingDirection), moveDirection:Dot(rightDirection)
				local isPressingLeftOrRight = UserInputService:IsKeyDown(Enum.KeyCode.A) 
					or UserInputService:IsKeyDown(Enum.KeyCode.D)
				if forwardDotProduct > 0.5 then
					targetSpeed = maxForwardSpeed
					if jumpValueHolder and isPressingLeftOrRight then
						local potentialBunnyHopSpeed = maxForwardSpeed + (velocityIndex / 1.75)
						targetSpeed = math.min(potentialBunnyHopSpeed, jumpSpeedCap)
					end
				elseif math.abs(sidewaysDotProduct) > 0.5 then
					targetSpeed = maxForwardSpeed * 0.7
					if jumpValueHolder and forwardDotProduct < 0.5 then
						jumpValueHolder = false
					end
				else
					targetSpeed = maxBackwardsSpeed
					if jumpValueHolder then jumpValueHolder = false end
				end
			else targetSpeed = initialSpeed end
			self.Humanoid.WalkSpeed = self.Humanoid.WalkSpeed + 
				(targetSpeed - self.Humanoid.WalkSpeed) * lerpRate
		end
	end)

Let me know if you need more information!
If someone can help me fixing this issue, that would be really appreciated!
Many Thanks.

uh im not too sure wether this will help but this script helps with detecting the direction that the player is moving, it uses the core scripts inside the plr scripts or smthing. i guess this will kinda fix the walk thing:

local ControlModule = require(player:WaitForChild("PlayerScripts").PlayerModule:WaitForChild("ControlModule"))

local MoveVector = ControlModule:GetMoveVector()
	if MoveVector.Z < 0 and MoveVector.X > 0 then
		return "forward-right"
	elseif MoveVector.Z < 0 and MoveVector.X < 0 then
		return "forward-left"
	elseif MoveVector.Z > 0 and MoveVector.X > 0 then
		return "backward-right"
	elseif MoveVector.Z > 0 and MoveVector.X < 0 then
		return "backward-left"	
	elseif MoveVector.Z == 0 and MoveVector.X > 0 then
		return "right"
	elseif MoveVector.Z == 0 and MoveVector.X < 0 then
		return "left"
	elseif MoveVector.Z > 0 and MoveVector.X == 0 then
		return "backward"
	elseif MoveVector.Z < 0 and MoveVector.X == 0 then
	    return "forward"
	else
		return "forward"
	end
1 Like

Thanks, but it’s not the issue.
Read the post again :slight_smile:

Edit: I guess the problem is in the Bunnyhop System. But I don’t really know, I should double check the script probably.

Two things: What is “backwards” in your system? Your code should work if your movement is like shift-lock mode, where the character literally walks backwards while staying facing away from the camera. If your controller is like default Roblox movement, and the character turns to face the camera when you press ‘S’ (or down on thumbstick), then their character is going to turn and they’re technically not walking ‘backwards’ in their frame of reference, they are walking forwards, just towards the camera. Depending on which of these scenarios you have, you may also need to consider the Camera.CFrame.LookVector.

Secondly, in your code, I think all you should need is one dot product, not forward and right. That one dot product will be 1 when going forward, some range like -0.7 to 0.7 when there is a sideways component, and anything less that the lower sideways bound (e.g. -0.7) would be backwards. You only need the rightVector dot product if you need some different behavior when moving right vs left.

1 Like

It’s like when player goes back by pressing S. And the walk speed goes to 16, or lower. (Forgot to mention that, this game is in First Person)

I didn’t know that you can check if player goes sideways, by using only one dot product. I’ll try implementing it. But I still have the same issue, from which I came here. :frowning:

Edit: I did like you said with one dot product, it’s working, but I didn’t understand why can’t I left RightVector dot product?

Your code wants left-right symmetrical behavior, and the only extra information you get from the RightVector dot product is whether the sideways movement is to the left (negative dot prod) or right (positive). You just call math.abs on it, so it doesn’t matter.

The LookVector dot product cannot tell left from right, but it still gives you information about how much the character is moving forwards vs sideways. Going straight forward, it will be 1, going full left or right, it will be 0, and going straight backwards it will be -1 (if actually walking backwards without turning around). Forward diagonal at 45 degress, either forward left or forward right, will have dot product of sqrt(2)/2, approx 0.7071. backwards diagonals will give -0.7071. Dot product encodes enough to know all this, just not enough to distiguish left from right.

The more important thing for a movement system is making sure you are taking into account all 3 of: Character facing direction, camera look direction, and user input intent (moveDirection). These are 3 different parameters.

1 Like

I see, but the bunnyhop (dash) movement is now not working because of this. :frowning:
Edit: Fixed, I just forgot about this line: if jumpValueHolder and forwardDotProduct < 0.5 then lol

1 Like