Hello!
I am trying to make a Tank Control movement system, but instead of only being able to walk forward, backward, and turn left and right, I want it so u can walk Forward, Left, Right, Backward, but still have to turn in your desired direction.
I already tried an existing script (BELOW)
The “OnLeft” and “OnRight” functions fail to make you actually walk RIGHT or LEFT (just going right and left apposed to the WORLD), while the “OnForward” and “OnBackward” actually make you walk Forward and Backward apposed to your Character.
I tried making the “OnLeft” and “OnRight” functions myself by using the Forward and Backward functions as a base, but because I am here, they didn’t work correctly.
Here is the Script VVV
repeat
wait(1)
until game.Players.LocalPlayer.CharacterAdded or game:IsLoaded()
local player = game.Players.LocalPlayer
local RunService = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")
player.Character:FindFirstChildOfClass("Humanoid").AutoRotate = false
local ctrllockBind = player.PlayerScripts.CustomShiftLock.SmoothShiftLock.EditConfig
local function DisableCtrlLock(bool:boolean)
if bool then
ctrllockBind:Fire("MANUALLY_TOGGLEABLE", false)
else
ctrllockBind:Fire("MANUALLY_TOGGLEABLE", true)
end
end
DisableCtrlLock(true)
local HRP
local function hrp_Update()
HRP = script.Parent.HumanoidRootPart
end
RunService.Heartbeat:Connect(hrp_Update)
local moving = false
local movingleft = false
local movingright = false
local movingback = false
local leftValue, rightValue, forwardValue, backwardValue = 0, 0, 0, 0
local Xdirection, Zdirection, rotation = 0, 0, 0
local rotation_speed = .3
local StatusModule = require(script.Parent.Animate.Status)
local function OnLeft (actionName, inputState)
if StatusModule.DoingSkill then
StatusModule.movement_Status.move_states.Left = false
movingleft = false
movingback = false
moving = false
Xdirection = 0
Zdirection = 0
return
end
if inputState == Enum.UserInputState.Begin then
if StatusModule.DoingSkill then return end
StatusModule.movement_Status.move_states.Left = true
movingleft = true
moving = false
movingback = false
while movingleft do
rotation = HRP.Orientation.Y
Xdirection = math.sin(0.1*rotation)*1
--Zdirection = math.cos(0.0174532925*rotation)
wait()
end
elseif inputState == Enum.UserInputState.End then
StatusModule.movement_Status.move_states.Left = false
movingleft = false
Xdirection = 0
Zdirection = 0
end
end
local function OnRight (actionName, inputState)
if StatusModule.DoingSkill then
StatusModule.movement_Status.move_states.Right = false
movingright = false
movingback = false
moving = false
Xdirection = 0
Zdirection = 0
return
end
if inputState == Enum.UserInputState.Begin then
if StatusModule.DoingSkill then return end
StatusModule.movement_Status.move_states.Right = true
movingright = true
moving = false
movingback = false
while movingright do
rotation = HRP.Orientation.Y
Xdirection = math.sin(0.1*rotation)*-1
--Zdirection = math.cos(0.0174532925*rotation)
wait()
end
elseif inputState == Enum.UserInputState.End then
StatusModule.movement_Status.move_states.Right = false
movingright = false
Xdirection = 0
Zdirection = 0
end
end
local function OnTurnLeft (actionName, inputState)
if StatusModule.DoingSkill then
StatusModule.movement_Status.rotate.Left = false
StatusModule.movement_Status.Turning = false
leftValue = 0
return
end
if inputState == Enum.UserInputState.Begin then
if StatusModule.DoingSkill then return end
StatusModule.movement_Status.move_states.Left = true
StatusModule.movement_Status.Turning = true
leftValue = rotation_speed
elseif inputState == Enum.UserInputState.End then
StatusModule.movement_Status.move_states.Left = false
StatusModule.movement_Status.Turning = false
leftValue = 0
end
end
local function OnTurnRight (actionName, inputState)
if StatusModule.DoingSkill then
StatusModule.movement_Status.move_states.Right = false
StatusModule.movement_Status.Turning = false
rightValue = 0
return
end
if inputState == Enum.UserInputState.Begin then
if StatusModule.DoingSkill then return end
StatusModule.movement_Status.move_states.Right = true
StatusModule.movement_Status.Turning = true
rightValue = rotation_speed
elseif inputState == Enum.UserInputState.End then
StatusModule.movement_Status.move_states.Right = false
StatusModule.movement_Status.Turning = false
rightValue = 0
end
end
local function OnForward (actionName, inputState)
if StatusModule.DoingSkill then
StatusModule.movement_Status.move_states.Forward = false
moving = false
Xdirection = 0
Zdirection = 0
return
end
if inputState == Enum.UserInputState.Begin then
if StatusModule.DoingSkill then return end
StatusModule.movement_Status.move_states.Forward = true
moving = true
movingback = false
while moving do
rotation = player.Character.HumanoidRootPart.Orientation.Y
Xdirection = math.sin(0.0174532925*rotation)*-1
Zdirection = math.cos(0.0174532925*rotation)*-1
wait()
end
--forwardValue = 1
elseif inputState == Enum.UserInputState.End then
StatusModule.movement_Status.move_states.Forward = false
moving = false
Xdirection = 0
Zdirection = 0
--forwardValue = 0
end
end
local function OnBackward (actionName, inputState)
if StatusModule.DoingSkill then
StatusModule.movement_Status.move_states.Backward = false
movingback = false
Xdirection = 0
Zdirection = 0
return
end
if inputState == Enum.UserInputState.Begin then
if StatusModule.DoingSkill then return end
StatusModule.movement_Status.move_states.Backward = true
movingback = true
moving = false
while movingback do
rotation = player.Character.HumanoidRootPart.Orientation.Y
Xdirection = math.sin(0.0174532925*rotation)*-1*-1
Zdirection = math.cos(0.0174532925*rotation)*-1*-1
wait()
end
elseif inputState == Enum.UserInputState.End then
StatusModule.movement_Status.move_states.Backward = false
movingback = false
Xdirection = 0
Zdirection = 0
end
end
local function OnQuickTurn (actionName, inputState)
if StatusModule.DoingSkill then return end
if inputState == Enum.UserInputState.Begin then
player.Character.HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad( 180 ), 0)
elseif inputState == Enum.UserInputState.End then
Xdirection = 0
Zdirection = 0
end
end
local function DirectionCheck()
return (script.Parent.HumanoidRootPart.CFrame.LookVector * script.Parent.Humanoid.MoveDirection.Z + script.Parent.HumanoidRootPart.CFrame.RightVector * script.Parent.Humanoid.MoveDirection.X).Unit
end
local MovementDirectionpart = Instance.new("Part", player.Character) ; MovementDirectionpart.Name = "MoveDirectionPart"
MovementDirectionpart.Anchored = true
MovementDirectionpart.CanCollide = false
MovementDirectionpart.CanTouch = false
MovementDirectionpart.Transparency = 0
MovementDirectionpart.Size = Vector3.new(1.5,1.5,1.5)
--this function V is for a part that will check the direction of your movement
local function PartDirections(Direction)
local PartVector = {
{Vector = Vector3.new(0, 0, 1), Move = CFrame.new(0,0,-15)}, --forwards
{Vector = Vector3.new(0, 0, -1), Move = CFrame.new(0, 0, 15)}, --backwards
{Vector = Vector3.new(-1, 0, 0), Move = CFrame.new(-15, 0, 0)}, --Left
{Vector = Vector3.new(1, 0, 0), Move = CFrame.new(15, 0, 0)}, --Right
}
local PartVector1 = {
{Vector = Vector3.new(-1, 0, -1), Move = CFrame.new(-15, 0, 15)},
{Vector = Vector3.new(-1, 0, 1), Move = CFrame.new(-15, 0, -15)},
{Vector = Vector3.new(1, 0, -1), Move = CFrame.new(15, 0, 15)},
{Vector = Vector3.new(1, 0, 1), Move = CFrame.new(15, 0, -15)},
}
for _,MoveDir in pairs(PartVector) do
if Direction:Dot(MoveDir.Vector) > .75 then
return MoveDir.Move
end
end
for _,MoveDir in pairs(PartVector1) do
if Direction:Dot(MoveDir.Vector) > .75 then
return MoveDir.Move
end
end
return nil
end
local function OnUpdate()
if player.Character and player.Character:FindFirstChild("Humanoid") then
player.Character.HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad( (leftValue - rightValue) * 2 ), 0)
script.Parent.Humanoid:Move( Vector3.new(Xdirection, 0, Zdirection), false )
end
end
local heartbeat = nil
heartbeat = game["Run Service"].Heartbeat:Connect(function(dt)
local fixedDT = 60 * (dt)
local fixedDirection = DirectionCheck()
local partDirection = PartDirections(fixedDirection)
if fixedDirection.Magnitude > 0 then
MovementDirectionpart.CFrame = script.Parent.Humanoid.RootPart.CFrame*partDirection
else
MovementDirectionpart.CFrame = script.Parent.Humanoid.RootPart.CFrame
end
end)
RunService:BindToRenderStep("Control", Enum.RenderPriority.Input.Value, OnUpdate)
ContextActionService:BindAction("TurnLeft", OnTurnLeft, true, Enum.KeyCode.Q, Enum.KeyCode.ButtonR1)
ContextActionService:BindAction("TurnRight", OnTurnRight, true, Enum.KeyCode.E, Enum.KeyCode.ButtonL1)
ContextActionService:BindAction("Left", OnLeft, true, Enum.KeyCode.A)
ContextActionService:BindAction("Right", OnRight, true, Enum.KeyCode.D)
ContextActionService:BindAction("Forward", OnForward, true, Enum.KeyCode.W, Enum.KeyCode.Up, Enum.KeyCode.DPadUp)
ContextActionService:BindAction("Backward", OnBackward, true, Enum.KeyCode.S, Enum.KeyCode.Down, Enum.KeyCode.DPadDown)
ContextActionService:SetTitle("Backward", "Move Backward")
--ContextActionService:BindAction("QuickTurn", OnQuickTurn, true, Enum.KeyCode.LeftControl, Enum.KeyCode.RightControl, Enum.KeyCode.ButtonR1)
If you have a Fix, or have any suggestions for me to make, I’ll be happy to chat and discuss with you!