I’m making this flight script, and i’ve got one problem and one question
My problem is the forward animation only plays when I move right, every other direction just plays the idle animation.
My question is how can I get the player to rotate with the camera?
Heres the code:
--//Services
local InputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
--//Instances
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded
local HRP = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")
local Camera = workspace.CurrentCamera
local DefFOV = Camera.FieldOfView
local Stopped = Vector3.new(0, 0, 0)
local Force = 110000
local StartHeight = 1000
local IdleAnim = Humanoid:LoadAnimation(script:WaitForChild("Idle"))
local ForwardAnim = Humanoid:LoadAnimation(script:WaitForChild("Forward"))
local bv = Instance.new("BodyVelocity", HRP)
bv.MaxForce = Stopped
bv.P = 10000
bv.Velocity = Stopped
bv.Name = "FlightVelocity"
local bp = Instance.new("BodyPosition", HRP)
bp.MaxForce = Stopped
bp.D = 10000
bp.P = 10000
bp.Name = "FlightPosition"
local bg = Instance.new("BodyGyro", HRP)
bg.MaxTorque = Stopped
bg.P = 10000
bg.D = 10^4
bg.Name = "FlightGyro"
local Info = TweenInfo.new(2)
local Info2 = TweenInfo.new(0.7)
local FlightKey = Enum.KeyCode.F
local BoostKey = Enum.KeyCode.LeftShift
--//Data
local Flying = false
local TakeOff = false
local Boosted = false
local IsMoving = false
local BaseSpeed = 220
local BoostedSpeed = 600
local Directions = {Forward = 0, Backward = 0, Left = 0, Right = 0}
local Slowdown = TweenService:Create(bv, Info2, {Velocity = Stopped})
local ZoomIn = TweenService:Create(Camera, Info2, {FieldOfView = DefFOV})
local ZoomOut = TweenService:Create(Camera, Info2, {FieldOfView = 80})
--//Functions
function Fly()
if not Flying then
if Humanoid.FloorMaterial ~= Enum.Material.Air then
TakeOff = true
IdleAnim:Play()
wait(.1)
bp.MaxForce = Vector3.new(110000, 110000, 110000)
bp.Position = HRP.Position * Vector3.new(0, StartHeight, 0)
end
Humanoid.PlatformStand = true
wait(0.25)
TakeOff = false
bp.MaxForce = Stopped
bv.MaxForce = Vector3.new(Force, Force, Force)
bg.MaxTorque = Vector3.new(Force, Force, Force)
IdleAnim:Play()
Flying = true
else
bv.MaxForce = Stopped
bg.MaxTorque = Stopped
bp.MaxForce = Stopped
IdleAnim:Stop()
ForwardAnim:Stop()
Slowdown:Cancel()
ZoomOut:Cancel()
ZoomIn:Play()
Boosted = false
IsMoving = false
Humanoid.PlatformStand = false
Flying = false
end
end
--//Events
--//Script
InputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == FlightKey then
Fly()
end
if input.KeyCode == Enum.KeyCode.W and not TakeOff then
Directions.Forward = 1
elseif input.KeyCode == Enum.KeyCode.A and not TakeOff then
Directions.Left = 1
elseif input.KeyCode == Enum.KeyCode.S and not TakeOff then
Directions.Backward = 1
elseif input.KeyCode == Enum.KeyCode.D and not TakeOff then
Directions.Right = 1
end
if input.KeyCode == BoostKey and Flying then
Boosted = true
end
end)
InputService.InputEnded:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.W then
Directions.Forward = 0
elseif input.KeyCode == Enum.KeyCode.A then
Directions.Left = 0
elseif input.KeyCode == Enum.KeyCode.S then
Directions.Backward = 0
elseif input.KeyCode == Enum.KeyCode.D then
Directions.Right = 0
end
end)
RunService.RenderStepped:Connect(function()
if Flying then
local Speed = (Boosted and BoostedSpeed or BaseSpeed)
bg.CFrame = Camera.CFrame
if IsMoving == false then
Boosted = false
end
if Directions.Forward == 1 then
bv.Velocity = Camera.CFrame.LookVector * Speed
ZoomOut:Play()
ZoomIn:Cancel()
Slowdown:Cancel()
ForwardAnim:Play()
IdleAnim:Stop()
IsMoving = true
end
if Directions.Forward == 0 then
ZoomOut:Cancel()
ZoomIn:Play()
ForwardAnim:Stop()
Slowdown:Play()
IdleAnim:Play()
end
if Directions.Backward == 1 then
bv.Velocity = -Camera.CFrame.LookVector * Speed
ZoomOut:Play()
ZoomIn:Cancel()
Slowdown:Cancel()
ForwardAnim:Play()
IdleAnim:Stop()
IsMoving = true
end
if Directions.Backward == 0 then
ZoomOut:Cancel()
ZoomIn:Play()
ForwardAnim:Stop()
Slowdown:Play()
IdleAnim:Play()
end
if Directions.Left == 1 then
bv.Velocity = -Camera.CFrame.RightVector * Speed
ZoomOut:Play()
ZoomIn:Cancel()
Slowdown:Cancel()
ForwardAnim:Play()
IdleAnim:Stop()
IsMoving = true
end
if Directions.Left == 0 then
ZoomOut:Cancel()
ZoomIn:Play()
ForwardAnim:Stop()
Slowdown:Play()
IdleAnim:Play()
end
if Directions.Right == 1 then
bv.Velocity = Camera.CFrame.RightVector * Speed
ZoomOut:Play()
ZoomIn:Cancel()
Slowdown:Cancel()
ForwardAnim:Play()
IdleAnim:Stop()
IsMoving = true
end
if Directions.Right == 0 then
ZoomOut:Cancel()
ZoomIn:Play()
ForwardAnim:Stop()
Slowdown:Play()
IdleAnim:Play()
end
if Directions.Forward == 0 and Directions.Backward == 0 and Directions.Left == 0 and Directions.Right == 0 then
IsMoving = false
IdleAnim:Play()
ForwardAnim:Stop()
ZoomOut:Cancel()
ZoomIn:Play()
end
end
end)
If you have the solution to my problem or answer to my question please let me know. Also if you know any ways to improve the script please tell me. Thanks in advance!