Hey so I’ve been working on a flight script that allows players to fly around with a double space press. The flight works well, but I’m having an issue with the character rotation. Currently, when players look up or down, the character only rotates slightly (about -10 degrees as set in my script). I want the character to fully rotate to match the camera angle when looking up or down.
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local torso = character:WaitForChild("LowerTorso")
local mouse = player:GetMouse()
local cam = workspace.CurrentCamera
local toggle = false
local bodyVel
local bodyGyro
local rotationTween = nil
local defaultFOV = 70
local zoomFOV = 80
local zoomTweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local currentZoomTween = nil
local isZoomed = false
local moveKeys = {
[Enum.KeyCode.W] = false,
[Enum.KeyCode.A] = false,
[Enum.KeyCode.S] = false,
[Enum.KeyCode.D] = false,
}
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if moveKeys[input.KeyCode] ~= nil then
moveKeys[input.KeyCode] = true
end
end)
UserInputService.InputEnded:Connect(function(input, gameProcessed)
if gameProcessed then return end
if moveKeys[input.KeyCode] ~= nil then
moveKeys[input.KeyCode] = false
end
end)
local idleAnimTrack, moveAnimTrack
local function updateCameraZoom()
local isMoving = moveKeys[Enum.KeyCode.W] or moveKeys[Enum.KeyCode.A] or
moveKeys[Enum.KeyCode.S] or moveKeys[Enum.KeyCode.D]
if isMoving and not isZoomed and toggle then
if currentZoomTween then
currentZoomTween:Cancel()
end
currentZoomTween = TweenService:Create(cam, zoomTweenInfo, {FieldOfView = zoomFOV})
currentZoomTween:Play()
isZoomed = true
elseif not isMoving and isZoomed then
if currentZoomTween then
currentZoomTween:Cancel()
end
currentZoomTween = TweenService:Create(cam, zoomTweenInfo, {FieldOfView = defaultFOV})
currentZoomTween:Play()
isZoomed = false
end
end
local function startFlight()
toggle = true
local startAnim = Instance.new("Animation")
startAnim.AnimationId = "rbxassetid://107126242904989"
local playStart = humanoid:LoadAnimation(startAnim)
playStart:Play(0.3)
bodyVel = Instance.new("BodyVelocity")
bodyVel.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bodyVel.Velocity = Vector3.new(0, 0, 0)
bodyVel.Parent = torso
bodyGyro = Instance.new("BodyGyro")
bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
bodyGyro.D = 500
bodyGyro.Parent = torso
local idleAnim = Instance.new("Animation")
idleAnim.AnimationId = "rbxassetid://76577092136636"
idleAnimTrack = humanoid:LoadAnimation(idleAnim)
local moveAnim = Instance.new("Animation")
moveAnim.AnimationId = "rbxassetid://75553511748832"
moveAnimTrack = humanoid:LoadAnimation(moveAnim)
idleAnimTrack.Priority = Enum.AnimationPriority.Action
moveAnimTrack.Priority = Enum.AnimationPriority.Action
idleAnimTrack.Looped = true
moveAnimTrack.Looped = true
defaultFOV = cam.FieldOfView
spawn(function()
while toggle do
local moveDirection = Vector3.new(0, 0, 0)
if moveKeys[Enum.KeyCode.W] then
moveDirection = moveDirection + cam.CFrame.LookVector
end
if moveKeys[Enum.KeyCode.S] then
moveDirection = moveDirection - cam.CFrame.LookVector
end
if moveKeys[Enum.KeyCode.A] then
moveDirection = moveDirection - cam.CFrame.RightVector
end
if moveKeys[Enum.KeyCode.D] then
moveDirection = moveDirection + cam.CFrame.RightVector
end
updateCameraZoom()
local lerpFactor = 0.1
if moveDirection.Magnitude > 0 then
local unitDir = moveDirection.Unit
local targetVelocity = unitDir * 50
bodyVel.Velocity = bodyVel.Velocity:Lerp(targetVelocity, lerpFactor)
local desiredCFrame = CFrame.new(torso.Position, torso.Position + unitDir) * CFrame.Angles(math.rad(-10), 0, 0)
bodyGyro.CFrame = bodyGyro.CFrame:Lerp(desiredCFrame, lerpFactor)
if rotationTween then
rotationTween:Cancel()
rotationTween = nil
end
if not moveAnimTrack.IsPlaying then
if idleAnimTrack and idleAnimTrack.IsPlaying then
idleAnimTrack:Stop(0.5)
end
moveAnimTrack:Play(0.5)
end
else
local targetVelocity = Vector3.new(0, 0, 0)
bodyVel.Velocity = bodyVel.Velocity:Lerp(targetVelocity, lerpFactor)
local targetCFrame = CFrame.new(torso.Position, torso.Position + cam.CFrame.LookVector)
local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
if rotationTween then
rotationTween:Cancel()
rotationTween = nil
end
rotationTween = TweenService:Create(bodyGyro, tweenInfo, { CFrame = targetCFrame })
rotationTween:Play()
if not idleAnimTrack.IsPlaying then
if moveAnimTrack and moveAnimTrack.IsPlaying then
moveAnimTrack:Stop(0.5)
end
idleAnimTrack:Play(0.5)
end
end
wait()
end
end)
end
local function stopFlight()
toggle = false
if bodyVel then
bodyVel:Destroy()
bodyVel = nil
end
if bodyGyro then
bodyGyro:Destroy()
bodyGyro = nil
end
if rotationTween then
rotationTween:Cancel()
rotationTween = nil
end
if idleAnimTrack then idleAnimTrack:Stop() end
if moveAnimTrack then moveAnimTrack:Stop() end
if isZoomed then
if currentZoomTween then
currentZoomTween:Cancel()
end
currentZoomTween = TweenService:Create(cam, zoomTweenInfo, {FieldOfView = defaultFOV})
currentZoomTween:Play()
isZoomed = false
end
for _, track in ipairs(humanoid:GetPlayingAnimationTracks()) do
track:Stop(0.5)
end
local stopAnim = Instance.new("Animation")
stopAnim.AnimationId = "rbxassetid://107126242904989"
local playStop = humanoid:LoadAnimation(stopAnim)
playStop:Play(0.3)
end
local jumpKey = Enum.KeyCode.Space
local lastJumpTime = 0
local doubleJumpThreshold = 0.3
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == jumpKey then
local currentTime = tick()
if (currentTime - lastJumpTime) <= doubleJumpThreshold then
if not toggle then
startFlight()
else
stopFlight()
end
end
lastJumpTime = currentTime
end
end)
game:GetService("Players").LocalPlayer.Character.Humanoid.Died:Connect(function()
if currentZoomTween then
currentZoomTween:Cancel()
end
if toggle then
stopFlight()
end
end)