How to get the direction of where I need to fly

Hello everyone!

I’m trying to figure out how to get the direction of where I want to fly. I have a big portion of code but most of it (including orientation of where im looking with my camera) I took out as it’s not necessary here. Basically, without having to use UserInputService to determine if I want to go Front/Back/Left/Right, I need to know where I’m flying. I’d prefer using Humanoid.MoveDirection as I’ve seen it be used before, but if there is another way without individually listing them, I’m open to using that as well.

I don’t need any of the tilting/orientation math, just the position math

What I’m trying to achieve:

Aaaaaand my code:

local function toggleFlight()
	if FlightToggled == false then
		FlightToggled = true
		
		-- Set Humanoid Settings
		Humanoid.PlatformStand = true
		
		-- Local Variables
		local RootRigAttachment = HumanoidRootPart.RootRigAttachment
		
		local Position = Instance.new('AlignPosition')
		Position.Name = "FlightPosition"
		Position.Mode = Enum.PositionAlignmentMode.OneAttachment
		Position.Parent = HumanoidRootPart
		Position.Attachment0 = RootRigAttachment

		Connection = RunService.Heartbeat:Connect(function()
			if (Humanoid.MoveDirection).Magnitude >= .1 then -- If moving, then fly in the direction I'm moving
				-- No Clue what to do here
			else -- If not moving, then do not change the position of the AlignPosition 
				Position.Position = RootRigAttachment.WorldPosition
			end
		end)
	end
end
2 Likes

It’s pretty simple what you do is get the Cameras CFrame and take it’s LookVector.
So :

local function toggleFlight()
	if FlightToggled == false then
		FlightToggled = true
		
		-- Set Humanoid Settings
		Humanoid.PlatformStand = true
		
		-- Local Variables
		local RootRigAttachment = HumanoidRootPart.RootRigAttachment
		
		local Position = Instance.new('AlignPosition')
		Position.Name = "FlightPosition"
		Position.Mode = Enum.PositionAlignmentMode.OneAttachment
		Position.Parent = HumanoidRootPart
		Position.Attachment0 = RootRigAttachment

		Connection = RunService.Heartbeat:Connect(function()
			if (Humanoid.MoveDirection).Magnitude >= .1 then 
                     local MoveDirection = workspace.CurrentCamera.CFrame.Position + workspace.CurrentCamera.LookVector * (5) -- play around with the number a bit
--You code for moving Forward
			else 
				Position.Position = RootRigAttachment.WorldPosition
			end
		end)
	end
end
1 Like

I get this part to an extent, but that returns a CFrame, and I need a Vector3. I’m unsure how to know if the player is clicking W/A/S/D without creating a function for every time one of those is pressed, which seems tedious and like it would be bad practice. I don’t really know what I need, but it’s been done before and I’m not quite sure how.

1 Like

Oh I see. The Inputs for WASD can be taken from UserInputService a bit tedious but yeah.i will update the code to give you a vector3.

Edit: the codes been updated. Also you could learn from the Toolbox scripts for flying.

Here’s what I’m trying to understand kinda, but I just do not know how to apply it. I’d like to keep away from the UserInputService method as, though it is a solution, in hindsight it’s pretty bad one. How to know which direction Humanoid MoveDirection is going? - Help and Feedback / Scripting Support - DevForum | Roblox

1 Like

You can use the same code. What you wanna do is to take the direction for movement and then just add it to the CFrame the Player is Currently at.

So,

local MoveDirection =(Player.Character.MoveDirection * (5))+Player.Character.CFrame.Position

This way you don’t need UIS. Didn’t think of that before. For moving back and forth you just set your Orientation to the direction the camera is looking at. When you press S looking down u should move up and vise versa.

Doesn’t quite work, and gives me this, and heres the code:

if (Humanoid.MoveDirection).Magnitude >= .1 then -- If moving, then change 
				local MoveDirection = (Humanoid.MoveDirection * (5)) + Humanoid.MoveDirection
				Position.Position = MoveDirection

I can only go a little bit in any direction as well

1 Like

Bump to the initial post, still have yet to find a solution for this. :frowning:

Yay u got it working
Now for the movement you can just adjust the speed (it’s not a variable. It’s the number by which Humanoid.MoveDirection is getting multiplied to ). I had commented there before but it commented the whole line so i edited it.

Edit : Also sorry for your issues with code.i didn’t realise u were using AlignPosition the whole time

1 Like