Need an improved custom rig movement script

I have been suggested to use this for my custom rigged walking vehicle. Though it does not work very well as when you go in one direction it will cease control of all other axis and will constantly go in one direction. Could someone give me any help or improvement tips I can use, would be much appreciated.

uis.InputBegan:Connect(function(input) 
    if input.KeyCode == Enum.KeyCode.W then
         up = up + 1
    elseif input.KeyCode == Enum.KeyCode.S then
         down = down - 1
    elseif input.KeyCode == Enum.KeyCode.A then
         left = left + 1
    elseif input.KeyCode == Enum.KeyCode.D then
         right = right - 1
    end
end) 

rs.Heartbeat:Connect(function() 
   VehicleHumanoid:Move(Vector3.new(right + left, 0, up + down))
end)
3 Likes

I used ContextActionService in mine, and added/subtracted from the current MoveDirection dependening on the state - for Begin add in that direction, for End subtract in that direction.

Here’s what I got:

--// Dependencies
local ContextActionService = game:GetService("ContextActionService")

--// Variables
local Humanoid = script.Parent.Humanoid
local DirectionInputs = {Enum.KeyCode.W, Enum.KeyCode.A, Enum.KeyCode.S, Enum.KeyCode.D}
local DirectionIncrements = {
	W = {Begin = Vector3.new(-1,0,0), End = Vector3.new(1,0,0)};
	A = {Begin = Vector3.new(1,0,0), End = Vector3.new(-1,0,0)};
	S = {Begin = Vector3.new(-1,0,0), End = Vector3.new(1,0,0)};
	D = {Begin = Vector3.new(1,0,0), End = Vector3.new(-1,0,0)};
}

--// Functions
local function handleInput(name, state, input)
	local keyCode = input.KeyCode
	local moveDirection = Humanoid.MoveDirection

	Humanoid:Move(moveDirection + DirectionIncrements[keyCode.Name][state.Name])
	print(Humanoid.MoveDirection)
end

--// Binds
ContextActionService:BindActionAtPriority("CustomMovement", handleInput, Enum.ContextActionPriority.High.Value, false, unpack(DirectionInputs))

(This is placed in StarterCharacterScripts)

5 Likes

Also would there be a way to convert the Vector3.new(1,0,0) to a local vector of the vehicle so that it can go forward without being on the worlds axis. This would also allow it to turn without turning back

1 Like

Yes!

vehicle.PrimaryPart.CFrame:VectorToWorldSpace(movementDirection)

where vehicle.PrimaryPart is a part that is aligned with the vehicle, e.g. front face is forward, top face is up. If there’s no handy part that is oriented correctly, you can use an invisible, nonsolid, massless part, or use the WorldCFrame of an attachment rotated to align with the vehicle.