I wrote a flying script yesterday, everything is okay with it except for the movement.
after I collide with something my character starts to rotate:
https://gyazo.com/e0390410932c17df1b2212f6662076fa
local player = game.Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character:FindFirstChild("Humanoid")
local cam = workspace.CurrentCamera
local UserInputService = game:GetService("UserInputService")
local idleAnim = Character:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("FlyIdle"))
local forwardAnim = Character:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("FlyForward"))
local w = false
local a = false
local s = false
local d = false
local flying = false
local canFly = false
Humanoid.StateChanged:connect(function(old, new)
if new == Enum.HumanoidStateType.Freefall then
canFly = true
elseif new == Enum.HumanoidStateType.Landed then
canFly = false
end
end)
UserInputService.InputBegan:Connect(function(key, chat)
if chat then return end
if key.KeyCode == Enum.KeyCode.Space and (canFly or flying) then
if not Character or not Humanoid or not Character:IsDescendantOf(workspace) or
Humanoid:GetState() == Enum.HumanoidStateType.Dead then
return
end
if flying then
Character.Animate.Disabled = false
idleAnim:Stop()
forwardAnim:Stop()
Character.PrimaryPart:FindFirstChild("FlightForce"):Destroy()
elseif not flying then
for i,v in ipairs(Humanoid:GetPlayingAnimationTracks()) do
if tostring(v.Name) == "FallAnim" then
v:Stop()
end
end
Character.Animate.Disabled = true
idleAnim:Play()
print(Humanoid:GetPlayingAnimationTracks())
Character.HumanoidRootPart.CFrame = CFrame.new(Character.HumanoidRootPart.Position, Character.HumanoidRootPart.Position + cam.CFrame.LookVector)
local bv = Instance.new("BodyVelocity", Character.PrimaryPart)
bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
bv.Velocity = Vector3.new(0,0,0)
bv.Name = "FlightForce"
end
flying = not flying
end
if key.KeyCode == Enum.KeyCode.W then
w = true
elseif key.KeyCode == Enum.KeyCode.A then
a = true
elseif key.KeyCode == Enum.KeyCode.S then
s = true
elseif key.KeyCode == Enum.KeyCode.D then
d = true
end
end)
UserInputService.InputEnded:Connect(function(key)
if key.KeyCode == Enum.KeyCode.W then
w = false
elseif key.KeyCode == Enum.KeyCode.A then
a = false
elseif key.KeyCode == Enum.KeyCode.S then
s = false
elseif key.KeyCode == Enum.KeyCode.D then
d = false
end
end)
while task.wait() do
if flying then
Character.PrimaryPart:FindFirstChild("FlightForce").Velocity = Vector3.new(0,0,0)
forwardAnim:Stop()
if w then
Character.PrimaryPart:FindFirstChild("FlightForce").Velocity = cam.CFrame.LookVector * 100
forwardAnim:Play()
Character.HumanoidRootPart.CFrame = CFrame.new(Character.HumanoidRootPart.Position, Character.HumanoidRootPart.Position + cam.CFrame.LookVector)
end
if a then
Character.PrimaryPart:FindFirstChild("FlightForce").Velocity = cam.CFrame.RightVector * -100
forwardAnim:Play()
Character.HumanoidRootPart.CFrame = CFrame.new(Character.HumanoidRootPart.Position, Character.HumanoidRootPart.Position + cam.CFrame.LookVector)
end
if s then
Character.PrimaryPart:FindFirstChild("FlightForce").Velocity = cam.CFrame.LookVector * -100
forwardAnim:Play()
Character.HumanoidRootPart.CFrame = CFrame.new(Character.HumanoidRootPart.Position, Character.HumanoidRootPart.Position + cam.CFrame.LookVector)
end
if d then
Character.PrimaryPart:FindFirstChild("FlightForce").Velocity = cam.CFrame.RightVector * 100
forwardAnim:Play()
Character.HumanoidRootPart.CFrame = CFrame.new(Character.HumanoidRootPart.Position, Character.HumanoidRootPart.Position + cam.CFrame.LookVector)
end
end
end
but it came out buggy and randomly moving my character backward
it also won’t rotate the character when the player stops moving.
and the movement is just bad, if you can give me a way to fix it or an idea that I can write, it will be helpful
you can also test it yourself by putting the script in Starter Character Scripts