I’m trying to make a flying system but I can only make the player look forward when flying and not sideways like a superhero flight(diagonal).
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--//Client
local Player = Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character.Humanoid
local Root = Character.HumanoidRootPart
local Camera = workspace.CurrentCamera
--//Variables
local Flying
local Velocity = nil
local Orientation = nil
--//ReplicatedStorage
local Signals = ReplicatedStorage.Signals
local Remotes = Signals.Remotes
local function trackPart(cframe)
local part = Instance.new("Part")
part.Parent = workspace
part.Size = Vector3.new(0.25,0.25,6)
part.CFrame = cframe
part.Anchored = true
part.CanCollide = false
part.Transparency = 0.5
task.delay(0.1,part.Destroy,part)
end
local function Update()
local ViewportSize = Camera.ViewportSize
local centerPosition = Vector2.new(ViewportSize.X/2,ViewportSize.Y/2)
local mouseLocation = UserInputService:GetMouseLocation()
local cameraRay = Camera:ViewportPointToRay(centerPosition.X,centerPosition.Y)
Orientation.CFrame = CFrame.lookAt(Root.Position,Root.Position + cameraRay.Direction)
Velocity.VectorVelocity = cameraRay.Direction * 1e2
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
trackPart(Root.CFrame)
end
UserInputService.InputBegan:Connect(function(input,gpe)
if gpe then return end
if input.KeyCode == Enum.KeyCode.F then
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
Velocity = Instance.new("LinearVelocity")
Velocity.MaxForce = 1e6
Velocity.Attachment0 = Root.RootAttachment
Velocity.Parent = Character
Orientation = Instance.new("AlignOrientation")
Orientation.Attachment0 = Root.RootAttachment
Orientation.Responsiveness = 200
Orientation.MaxTorque = 1e6
Orientation.Mode = Enum.OrientationAlignmentMode.OneAttachment
Orientation.Parent = Character
Humanoid.CameraOffset = Vector3.new(0,1,3)
Remotes.ToServer:FireServer("EnableFlight")
Flying = RunService.Stepped:Connect(function()
Update()
end)
end
end)
UserInputService.InputEnded:Connect(function(input,gpe)
if gpe then return end
if input.KeyCode == Enum.KeyCode.F then
if Flying then Flying:Disconnect() Flying = nil end
if Velocity then Velocity:Destroy() end
if Orientation then Orientation:Destroy() end
Humanoid.CameraOffset = Vector3.zero
Remotes.ToServer:FireServer("DisableFlight")
end
end)
In the Update function I update the Orientation(AlignOrientation) to change where the player looks. I can’t find the perpendicular axis of the direction(cameraRay) relative to the root position.
I tried using cross and setting the y axis to 0 and changing it to Root.CFrame.UpVector.Y but nothing worked and only broke the system.