I'm trying to make the character fly when they double jump, and use WASD to control it. I'm almost done with it, but when I let go of the keys when I'm flying, I just zoop out of existence. If I lower the MaxTorque of the BodyGyro, my character just twitches in place. How would I handle this?
--It's a bit long but the problem is near the end of the script
UIS = game:GetService("UserInputService")
player = game.Players.LocalPlayer
repeat wait() until player.Character
torso = player.Character:WaitForChild("Torso")
humanoid = player.Character:WaitForChild("Humanoid")
mouse = player:GetMouse()
camera = workspace.CurrentCamera
--Variables
max_velocity = 60
acceleration = 12 --I'll incorporate this later
bgyro_angle = 60
--Double jump
space_presses = 0
press1 = 0
press2 = 0
UIS.InputBegan:connect(function(input)
if input.KeyCode == Enum.KeyCode.Space then
space_presses = math.min(space_presses + 1, 2)
if space_presses == 1 then
press1 = tick()
elseif space_presses == 2 then
press2 = tick()
if press2 - press1 > .3 then
space_presses = 1
press1 = tick()
else
onDoubleJump()
end
end
end
end)
--Direction vector from WASD
hovering = script:WaitForChild("Hovering") --A bool value
direction = Vector3.new()
key_vectors = {
W = {false, Vector3.new(0, 0, -1)},
A = {false, Vector3.new(-1, 0, 0)},
S = {false, Vector3.new(0, 0, 1)},
D = {false, Vector3.new(1, 0, 0)}
}
function GetVectorSum()
local dir = Vector3.new()
for _, key_vector in pairs(key_vectors) do
if key_vector[1] then
dir = dir + key_vector[2]
end
end
if dir == Vector3.new() then
hovering.Value = true
return dir
else
hovering.Value = false
return dir.unit
end
end
UIS.InputBegan:connect(function(input)
local vector = key_vectors[input.KeyCode.Name]
if vector then
vector[1] = true
direction = GetVectorSum()
end
end)
UIS.InputEnded:connect(function(input)
local vector = key_vectors[input.KeyCode.Name]
if vector then
vector[1] = false
direction = GetVectorSum()
end
end)
--Flying
flying = script:WaitForChild("Flying") --Another bool value
huge_vector = Vector3.new(math.huge, math.huge, math.huge)
bgyro = Instance.new("BodyGyro")
bgyro.MaxTorque = huge_vector
bvel = Instance.new("BodyVelocity")
bvel.MaxForce = huge_vector
bpos = Instance.new("BodyPosition")
bpos.MaxForce = huge_vector
hovering.Changed:connect(function()
if flying.Value then
if hovering.Value then
bvel.Parent = nil
bpos.Position = torso.Position
bpos.Parent = torso
while flying.Value and hovering.Value and wait() do --Here's the problem
bgyro.CFrame = CFrame.new(torso.Position, mouse.Hit.p)
end
else
bpos.Parent = nil
bvel.Parent = torso
while flying.Value and not hovering.Value and wait() do
bgyro.CFrame = CFrame.new(torso.Position, torso.Position + camera.CFrame:vectorToWorldSpace(direction) * 10) * CFrame.Angles(-math.rad(bgyro_angle), 0, 0)
end
end
end
end)
function onDoubleJump()
flying.Value = not flying.Value
if flying.Value then
humanoid.PlatformStand = true
bvel.Parent = torso
bgyro.Parent = torso
while flying.Value and wait() do
bvel.Velocity = camera.CFrame:vectorToWorldSpace(direction) * max_velocity
end
else
humanoid.PlatformStand = false
bpos.Parent = nil
bvel.Parent = nil
bgyro.Parent = nil
end
end
