I am trying to make a simple flight script, yet as I want the character to face the direction the camera does, I have to make it use one of the humanoid state types which doesn’t auto-rotate. The issue is that whenever I crash into anything, I spin around like crazy. There are many other posts related to this but I have tried all the methods I could find (such as using a bodygyro) yet it still doesn’t seem to help.
Code below (kinda messy since its a mix of my code, some random tutorial, and some devforum posts)
local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hrp = char:WaitForChild("HumanoidRootPart")
local hum = char:WaitForChild("Humanoid")
local camera = game.Workspace.CurrentCamera
local LastTapped, Tapped = false, false
local flyUpSpeed = 20
local flyDownSpeed = 20
local wdown = false
local f1 = false
local f2 = false
local f3 = false
local f4 = false
local speed = 0.5
local bp = nil
local bodyVel = nil
local v1 = Vector3.new(0, 0, 0)
local v2 = Vector3.new(0, 0, 0)
local v3 = Vector3.new(0, 0, 0)
local v4 = Vector3.new(0, 0, 0)
bp = Instance.new("BodyPosition",hrp)
bp.MaxForce = Vector3.new()
bp.D = 100
bp.Position = hrp.Position + Vector3.new(0,10,0)
bp.MaxForce = Vector3.new(400000,400000,400000)
bodyVel = Instance.new("BodyVelocity", hrp)
bodyVel.Name = "BodyVel"
bodyVel.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
local anim = hum:FindFirstChild("Animator") or Instance.new("Animator")
anim.Parent = hum
local forwardAnim = Instance.new("Animation")
forwardAnim.AnimationId = "rbxassetid://13432138726"
local rightAnim = Instance.new("Animation")
rightAnim.AnimationId = "rbxassetid://13432355116"
local leftAnim = Instance.new("Animation")
leftAnim.AnimationId = "rbxassetid://13432373743"
local backAnim = Instance.new("Animation")
backAnim.AnimationId = "rbxassetid://13432412550"
local forward = anim:LoadAnimation(forwardAnim)
local right = anim:LoadAnimation(rightAnim)
local left = anim:LoadAnimation(leftAnim)
local back = anim:LoadAnimation(backAnim)
hum.PlatformStand = true
uis.InputBegan:connect(function(Input, GPE)
if GPE then return end
if Input.KeyCode == Enum.KeyCode.W then
f1 = true
forward:Play()
right:Stop(); left:Stop(); back:Stop()
--hum:ChangeState(Enum.HumanoidStateType.PlatformStanding)
if bp then
bp:Destroy()
bp = nil
end
while f1 and wait() do
v1 = camera.CFrame.LookVector*speed*100
end
forward:Stop()
end
if Input.KeyCode == Enum.KeyCode.A then
f2 = true
left:Play()
right:Stop(); forward:Stop(); back:Stop()
--hum:ChangeState(Enum.HumanoidStateType.FallingDown)
if bp then
bp:Destroy()
bp = nil
end
while f2 and wait() do
v2 = camera.CFrame.RightVector*speed*-100
end
end
if Input.KeyCode == Enum.KeyCode.D then
f3 = true
right:Play()
forward:Stop(); left:Stop(); back:Stop()
--hum:ChangeState(Enum.HumanoidStateType.FallingDown)
if bp then
bp:Destroy()
bp = nil
end
while f3 and wait() do
v3 = camera.CFrame.RightVector*speed*100
end
end
if Input.KeyCode == Enum.KeyCode.S then
f4 = true
back:Play()
right:Stop(); left:Stop(); forward:Stop()
--hum:ChangeState(Enum.HumanoidStateType.FallingDown)
if bp then
bp:Destroy()
bp = nil
end
while f4 and wait() do
v4 = camera.CFrame.LookVector*speed*-100
end
end
end)
uis.InputEnded:Connect(function(Input)
print("hi")
if Input.KeyCode == Enum.KeyCode.W then
f1 = false
wait()
v1 = Vector3.new(0, 0, 0)
print(bp)
if not bp and not f2 and not f3 and not f4 then
bp = Instance.new("BodyPosition",hrp)
bp.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
bp.D = 100
bp.Position = hrp.Position
end
elseif Input.KeyCode == Enum.KeyCode.A then
f2 = false
wait()
v2 = Vector3.new(0, 0, 0)
if not bp and not f1 and not f3 and not f4 then
bp = Instance.new("BodyPosition",hrp)
bp.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
bp.D = 100
bp.Position = hrp.Position
end
elseif Input.KeyCode == Enum.KeyCode.D then
f3 = false
wait()
v3 = Vector3.new(0, 0, 0)
if not bp and not f1 and not f2 and not f4 then
bp = Instance.new("BodyPosition",hrp)
bp.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
bp.D = 100
bp.Position = hrp.Position
end
elseif Input.KeyCode == Enum.KeyCode.S then
f4 = false
wait()
v4 = Vector3.new(0, 0, 0)
print(v4)
if not bp and not f1 and not f2 and not f3 then
bp = Instance.new("BodyPosition",hrp)
bp.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
bp.D = 100
bp.Position = hrp.Position
end
end
end)
print("hi")
while wait() do
bodyVel.Velocity = v1+v2+v3+v4
hrp.CFrame = CFrame.new(hrp.CFrame.p) * (camera.CFrame - camera.CFrame.p)
--print(camDir)
--print(hum:GetState())
end
for reference, im trying to get something similar to the flight in this game or this game
(what happens)
(what im trying to achieve)