What does this part mean in a flying script

I’m studying a flying script and this CFrame math part doesn’t make sense to me, specifically the align_position.Position math:

align_orientation.MaxTorque = 10000000
align_position.MaxForce = 1000000000
while isFlying do
task.wait()
align_orientation.CFrame = CFrame.new(camera.CFrame.Position, HumanoidRootPart.Position)
align_position.Position = HumanoidRootPart.Position + (HumanoidRootPart.Position - camera.CFrame.Position).unit * speed
end

can you explain this thoroughly to me and also the loop makes it so I go flying forward without stopping and I ideally want it so that it I only fly forward when I press W
if you want the whole script here it is:

local UIS = game:GetService(“UserInputService”)
local keybind = Enum.KeyCode.Q
local speed = 50

local isFlying = false
local local_player = game.Players.LocalPlayer
local character = local_player.Character
local HumanoidRootPart = character.HumanoidRootPart

local camera = game.Workspace.CurrentCamera

local function Start_flying()
local align_orientation = Instance.new(“AlignOrientation”, HumanoidRootPart)
align_orientation.Name = “align_orientation”
align_orientation.Attachment0 = HumanoidRootPart.RootAttachment
align_orientation.Mode = Enum.OrientationAlignmentMode.OneAttachment
align_orientation.Responsiveness = 50

local align_position = Instance.new(“AlignPosition”, HumanoidRootPart)
align_position.Name = “align_position”
align_position.Attachment0 = HumanoidRootPart.RootAttachment
align_position.Mode = Enum.PositionAlignmentMode.OneAttachment

align_orientation.MaxTorque = 10000000
align_position.MaxForce = 1000000000
while isFlying do
task.wait()
align_orientation.CFrame = CFrame.new(camera.CFrame.Position, HumanoidRootPart.Position)
align_position.Position = HumanoidRootPart.Position + (HumanoidRootPart.Position - camera.CFrame.Position).unit * speed
end
end

local function End_flying()

if HumanoidRootPart.align_orientation then
HumanoidRootPart.align_orientation:Destroy()
HumanoidRootPart.align_position:Destroy()

end

end

UIS.InputBegan:Connect(function(input, gameprocessed)
if gameprocessed then return end
if input.KeyCode == keybind then

  if isFlying then
  	
  	isFlying = false
  	character.Animate.Enabled = not isFlying
  	
  	End_flying()
  	
  elseif (not isFlying) then
  	isFlying = true
  	character.Animate.Enabled = not isFlying

  	Start_flying()
  	
  end

end

end)

.unit() makes a unit vector which is a vector3 that represents a direction. Unit vectors are of magnitude 1. Add this unit vector to the hrp position and it will move it 1 stud in that direction. Multiply the unit vector by number (speed) to make it add more which makes it faster.

1 Like

do you know why we subtract the camera’s position?

usually, when you subtract vectors, you are getting the distance between the two vectors

1 Like

When you subtract vectors, the result is another vector that connects the two ends of the vectors. You might have seen that in order to find position you need to do:

(posA - posB).Magnitude

If you think of position as a vector, where the start is at 0,0,0. Then finding the magnitude of a-b would get the distance between a and b.

In the flying script, instead of finding the magnitude of a-b you are finding the direction, so it uses .unit.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.