Trying to make a character fly script based on MoveDirection

I’ve been trying to make a simple fly script that goes off humanoid move direction but it doesn’t seem to work properly.
I’m assuming its cause the direction vector isn’t being rotated properly but I don’t really know how to do that so I’m posting this.

here’s the code:

local Player = game.Players.LocalPlayer
local Character = Player.Character

local Humanoid:Humanoid = Character.Humanoid
local RootPart:BasePart = Character.HumanoidRootPart
local Camera = workspace.CurrentCamera

local FlySpeed = 16

local FlyPosition = Instance.new("BodyPosition")
FlyPosition.MaxForce = Vector3.new(100,100,100)
FlyPosition.Position = RootPart.Position
FlyPosition.Parent = RootPart

local MoveDirection = Vector3.zero

local RunService = game:GetService("RunService")

RunService.RenderStepped:Connect(function(dt)
	MoveDirection = Camera.CFrame:VectorToObjectSpace(Humanoid.MoveDirection)
	FlyPosition.Position = RootPart.Position + MoveDirection * FlySpeed
end)
2 Likes

Instead of using CFrame:Vector ToObjectSpace you should use camer look and right vector to transform the movement direction

local Player = game.Players.LocalPlayer
local Character = Player.Character

local Humanoid = Character:WaitForChild("Humanoid")
local RootPart = Character:WaitForChild("HumanoidRootPart")
local Camera = workspace.CurrentCamera

local FlySpeed = 16

local FlyPosition = Instance.new("BodyPosition")
FlyPosition.MaxForce = Vector3.new(100, 100, 100)
FlyPosition.Position = RootPart.Position
FlyPosition.Parent = RootPart

local MoveDirection = Vector3.new()

local RunService = game:GetService("RunService")

RunService.RenderStepped:Connect(function(dt)
    local cameraLook = Camera.CFrame.LookVector
    local cameraRight = Camera.CFrame.RightVector
    
    MoveDirection = (cameraRight * Humanoid.MoveDirection.X) + (cameraLook * Humanoid.MoveDirection.Z)
    MoveDirection = Vector3.new(MoveDirection.X, 0, MoveDirection.Z).Unit
    
    if Humanoid.Jump then
        MoveDirection = MoveDirection + Vector3.new(0, 1, 0)
    end
    
    FlyPosition.Position = RootPart.Position + MoveDirection * FlySpeed
end)

this isn’t working, now it just flings me out of the universe.

Here is a good comprehensive tutorial made by @5uphi which uses humanoid.MoveDirection to move.

For explanations why your script might not work

  1. Max force might be too low
  2. Humanoid physics will interfere with the force and apply an opposing forces which is why the tutorial sets humaonid to physics mode to disable the humanoid.