Hello. I am trying to make a flying script to move around in zero gravity conditions. The strange problem that I am facing is:
I put a BodyForce that pushes the character around. After some time i remove that force, and being in zero gravity conditions, because of it’s inertia it should continue moving constantly.
But, when i remove the force it continues to move constantly, but only on the y axis; on the x and z axis it stops completely.
I thought it was the ControlModue that was doing that, so i tried disabling it, but it didn’t work.
Please help me solve this problem.
local uis = game:GetService("UserInputService")
local LastPressed = 0
local PressedTime = 0
local JumpForce = 0
local JMPS = 2
-- will be used to start the fly funtion
local flying = false -- Determines if you're in flight or not
local friction = 1 -- not used yet
local PlayerModule = require(game.Players.LocalPlayer.PlayerScripts:WaitForChild("PlayerModule"))
local KeyControlModule = require(game.Players.LocalPlayer.PlayerScripts:WaitForChild("PlayerModule"):WaitForChild("ControlModule"))
local Controls = PlayerModule:GetControls()
--got the modules so i can disable them in the future
uis.InputBegan:connect(function(input)
if input.KeyCode == Enum.KeyCode.Space then
LastPressed = tick()
end
end)
uis.InputEnded:connect(function(input)
if input.KeyCode == Enum.KeyCode.Space then
PressedTime = tick() - LastPressed
if PressedTime <= 0.1 then
JumpForce = 1
else
JumpForce = PressedTime * 10 *JMPS
if flying == true then
endFlying()
else
fly()
end
end
--enables the fly in zero gravity environment function
elseif input.KeyCode == Enum.KeyCode.E then
Levitate() --creates the zero gravity environment
end
end)
function GetMassOfModel(model)
local mass = 0
for i, v in pairs(model:GetChildren()) do
if v:IsA('BasePart')or v:IsA("MeshPart") then
mass = mass + v:GetMass()
end
end
return mass
end -- mass calculator
local myPlayer = game.Players.LocalPlayer
local myChar = myPlayer.Character or myPlayer.CharacterAdded:Wait()
local myHRP = myChar:WaitForChild("HumanoidRootPart")
local Mouse = myPlayer:GetMouse()
local Humanoid = myChar:WaitForChild("Humanoid")
Humanoid.JumpPower = 0;
local bf = Instance.new("BodyForce", myHRP) -- Body Force so we can move around
bf.Force = Vector3.new()
local Levitation = Instance.new("BodyForce", myHRP) -- a Body force used to levitate
Levitation.Force = Vector3.new()
local CounterLevitation = Instance.new("BodyForce", myHRP) -- a Body force used to levitate
CounterLevitation.Force = Vector3.new()
local bg = Instance.new("BodyGyro", myHRP) -- Body Gyro that determines your rotation
bg.MaxTorque = Vector3.new()
bg.D = 10
local rs = game:GetService("RunService") -- Look me up
local camera = game.Workspace.CurrentCamera
function Levitate()
Levitation.Force = Vector3.new(1, workspace.Gravity * GetMassOfModel(myChar),1)
CounterLevitation.Force = Vector3.new(-1, 0,-1)
-- Made a force that counters the gravitation
end-- the zero gravity environment creator function
function fly()
flying = true
bg.MaxTorque = Vector3.new(400000,400000,400000)
local Direction = Mouse.Hit * 10
-- saving the direction you were originaly faced.
KeyControlModule:Disable()
bf.Force = Direction * JumpForce * 10
bg.CFrame = CFrame.new(bg.Parent.Position, Direction * JumpForce)
wait(0.4)
bf.Force = Vector3.new(0,0,0)
end
function endFlying()
--Levitation.Force = Vector3.new(0,0,0)
bg.MaxTorque = Vector3.new(0,0,0)
bf.Force = Vector3.new(0,0,0)
KeyControlModule:Enable()
flying = false
speed = 1
end