Help on flight system

I’ve made a flight system, but I can only move on one axis. I’m pretty sure the issue is HRP.CFrame.LookVector, but I’m not sure what to do.

local player = game.Players.LocalPlayer
local char = player.Character
local HRP = char:WaitForChild("HumanoidRootPart")

local RS = game:GetService("RunService")
local CAS = game:GetService("ContextActionService")
local UIS = game:GetService("UserInputService")

local LV = script.LinearVelocity
local speed = 50
local flying = true

local clone = LV:Clone()
clone.Name = "LV"

function Fly(action, state)
	if state == Enum.UserInputState.Begin then
		local conn = nil
		if flying then
			local att = Instance.new("Attachment")
			att.Name = "Att"
			att.Parent = HRP
			clone.Attachment0 = att
			clone.Parent = HRP
			conn = RS.RenderStepped:Connect(function()
				local vel = Vector3.new(0,0,0)
				if UIS:IsKeyDown(Enum.KeyCode.W) then
					vel += Vector3.new(0,0,speed) * HRP.CFrame.LookVector
				elseif UIS:IsKeyDown(Enum.KeyCode.S) then
					vel += Vector3.new(0,0,-speed) * HRP.CFrame.LookVector
				end
				clone.VectorVelocity = vel
			end)
			flying = false
		else
			if HRP:FindFirstChild("LV") and HRP:FindFirstChild("Att") then
				HRP.LV.VectorVelocity = Vector3.new(0,0,0)
				HRP.Att:Destroy()
				if conn ~= nil then conn:Disconnect() end
			end
			flying = true
		end
	end
end

CAS:BindAction("Fly", Fly, true, Enum.KeyCode.E)

You are removing the rest of the axis by multiplying by 0,0 making the X and Z axis zero.

I believe you meant scalar multiplication instead.

HRP.CFrame.LookVector*speed
2 Likes

Thanks, but how could I do that for left/right (A/D)? I can only move on one axis there, too.

if UIS:IsKeyDown(Enum.KeyCode.W) then
					vel += HRP.CFrame.LookVector*speed
				elseif UIS:IsKeyDown(Enum.KeyCode.A) then
					vel += Vector3.new(-speed)
				elseif UIS:IsKeyDown(Enum.KeyCode.S) then
					vel += HRP.CFrame.LookVector*-speed
				elseif UIS:IsKeyDown(Enum.KeyCode.D) then
					vel += Vector3.new(speed)
				end

Never mind, I forgot RightVector exists.