How to prevent the player (Cannon) from moving forward

In a mob control-esque game that I’m creating as commission, I’ve been working on the simple movement as seen in this script:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local RunService = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")
local Left, Right = 0, 0
local function onUpdate()
	local Direction = Right-Left
	Character.Humanoid:Move(Vector3.new(-Direction, 0, 0), false)
end

local function onLeft(Key, State)
	if State == Enum.UserInputState.Begin then
		Left = 1
	else
		Left = 0
	end
end

local function onRight(Key, State)
	if State == Enum.UserInputState.Begin then
		Right = 1
	else
		Right = 0
	end
end


	
RunService:BindToRenderStep("Control", Enum.RenderPriority.Input.Value, onUpdate)

ContextActionService:BindAction("Left", onLeft, true, "a", Enum.KeyCode.Left, Enum.KeyCode.DPadLeft)
ContextActionService:BindAction("Right", onRight, true, "d", Enum.KeyCode.Right, Enum.KeyCode.DPadRight)

However, the issue comes from when the player changes direction, as seen here:

As shown, the player moves forward through the Z axis quite notably, and that becomes a problem. I believe it may have something to do with the physics engine, but I am unsure as of right now.

1 Like

Not sure if this is what you want but you could Anchor the HumanoidRootPart and then in your Update function set the CFrame of your character’s PrimaryPart with the new position.

Following your advice, I changed my code to this:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local RunService = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")
local Left, Right = 0, 0
local function onUpdate()
	local Direction = Right-Left
	if (Character.HumanoidRootPart.CFrame + Vector3.new(-Direction/2.5, 0, 0)).X <= (workspace.Map1.Border1.Position.X - workspace.Map1.Border1.Size.X / 2) and (Character.HumanoidRootPart.CFrame + Vector3.new(-Direction/2.5, 0, 0)).X >= (workspace.Map1.Border2.Position.X + workspace.Map1.Border2.Size.X / 2) then
		Character.HumanoidRootPart.CFrame = Character.HumanoidRootPart.CFrame + Vector3.new(-Direction/2.5, 0, 0)
	end
end

local function onLeft(Key, State)
	if State == Enum.UserInputState.Begin then
		Left = 1
	else
		Left = 0
	end
end

local function onRight(Key, State)
	if State == Enum.UserInputState.Begin then
		Right = 1
	else
		Right = 0
	end
end


	
RunService:BindToRenderStep("Control", Enum.RenderPriority.Input.Value, onUpdate)

ContextActionService:BindAction("Left", onLeft, true, "a", Enum.KeyCode.Left, Enum.KeyCode.DPadLeft)
ContextActionService:BindAction("Right", onRight, true, "d", Enum.KeyCode.Right, Enum.KeyCode.DPadRight)

While not being affected by WalkSpeed, it works nonetheless, so thank you.

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