"Flying" script always moves player forwards

So I’m trying to make a flying script. I’m using MoveDirection to figure out where the humanoid is moving. The problem is, even if I’m trying to move backwards, this moves me forwards. Shouldn’t it automatically detect that I’m trying to go backwards because I’m reading MoveDirection? I’m using a BodyVelocity to move:

Velocity = hum.Humanoid.MoveDirection + game.Workspace.CurrentCamera.CoordinateFrame.lookVector * Vector3.new(10, 10, 10)

What do I need to do to make this work with all directions?

If the flying script is for your own character, you can use UserInputService and detect which WASD keys are being pressed… Based off the user input, you can set a BodyVelocity instance value to a certain value so that the player moves in the intended direction.

Note:

  • Pressing both the W and S key will make the player move backwards because the S key overrides the W key for player movement - same thing for the A and D keys.

Unfortunately, I want this cross-compatible with other platforms, so such a trick is out of the question :frowning:

Yes, I’m using BodyVelocity. The code I posted works, it just always moves me forward. So like, even if I press “S”, it moves me forward. That’s my problem.

Option 1

local p = game.Players.LocalPlayer

local c = p.Character

local hum = c.Humanoid

local hroot = c.HumanoidRootPart

local bv = Instance.new("BodyVelocity")
bv.Parent = hroot
bv.MaxForce = Vector3.new(99999, 99999, 99999)
bv.Velocity = Vector3.new(0, 0, 0)

while true do
	wait()
	local Velocity = hum.MoveDirection + game.Workspace.CurrentCamera.CoordinateFrame.lookVector * Vector3.new(10, 10, 10)
	bv.Velocity = Velocity
end

Option 2

local p = game.Players.LocalPlayer

local c = p.Character

local hum = c.Humanoid

local hroot = c.HumanoidRootPart

local bv = Instance.new("BodyVelocity")
bv.Parent = hroot
bv.MaxForce = Vector3.new(99999, 99999, 99999)
bv.Velocity = Vector3.new(0, 0, 0)

while true do
	wait()
	local Velocity = hum.MoveDirection* Vector3.new(10, 0, 10) + game.Workspace.CurrentCamera.CoordinateFrame.lookVector * Vector3.new(0, 10, 0)
	bv.Velocity = Velocity
end

If you want the character’s moving direction to really affect the velocity, use option 2…

You can open a blank studio, and paste an “option” into a local script… and try the other one after.

9 Likes

Ah yes, thank you. Option 2 had what I was looking for :slight_smile:

1 Like

how do i stop it flying after activating it
as it is currently bieng quite annoying to deactivate

@TheAviator01 how could i stop the flight and have it so the player still works

Looking at my terrible code above, if you set BodyVelocity.MaxForce = Vector3.new(0,0,0), then the player won’t fly around.

1 Like