What I want to achieve is to fix these 2 issues:
1: I wanna make it so that when the player turns the camera around while moving forward (say the camera turns backwards) the momentum they had going forward is slowed down before turning around and going to the other direction the camera is pointing at I want to add inertia.
2: When I am holding W and press F, BoolValue.FWD becomes false so when I press F while holding W I just stay in the air till I press W again which is a small bug but considering that this is a combat game everything has to be smooth
Any help would be very appreciated
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:FindFirstChild("Humanoid") :: Humanoid
local HRP = character:FindFirstChild("HumanoidRootPart") :: BasePart
local camera = game.Workspace.CurrentCamera
local Flying = false
local CanMove = true
local SpeedValue = 5 --// Will accelerate
local BackwardSpeedValue = 1
local SideSpeedValue = 3
local BoolValues = {
["FWD"] = false,
["BWD"] = false,
["RHT"] = false,
["LFT"] = false,
}
local connections = {}
local trashTable = {}
function initialLaunch()
local BV = Instance.new("BodyVelocity")
BV.Parent = HRP
BV.MaxForce = Vector3.new(10000,10000,10000)
BV.Velocity = HRP.CFrame.UpVector*250
task.wait(0.3)
BV:Destroy()
end
local FlightChecker = {
[false] = function()
Flying = true
print("fly")
local AlignOrient = Instance.new("AlignOrientation")
AlignOrient.Parent = HRP
trashTable["Orient"] = AlignOrient
AlignOrient.Mode = Enum.OrientationAlignmentMode.OneAttachment
AlignOrient.Attachment0 = HRP:FindFirstChild("RootAttachment")
AlignOrient.Responsiveness = 50
local AlignPos = Instance.new("AlignPosition")
AlignPos.Parent = HRP
trashTable["Position"] = AlignPos
AlignPos.Mode = Enum.PositionAlignmentMode.OneAttachment;
AlignPos.Attachment0 = HRP:FindFirstChild("RootAttachment")
AlignPos.Responsiveness = 50
AlignPos.MaxForce = 1000000
AlignOrient.MaxTorque = 1000000
AlignPos.Position = HRP.Position
AlignPos.Responsiveness = 50
print(BoolValues.FWD)
connections["Flight"] = RunService.RenderStepped:Connect(function(dt)
if HRP.CFrame.LookVector:Dot(camera.CFrame.LookVector) <= 0 then --// player has forward momentum that needs to be stopped before turning around
SpeedValue -= 0.3
SpeedValue = math.min(SpeedValue, 5)
print(SpeedValue)
if SpeedValue <= 5 then
print("can rotate")
end
AlignOrient.CFrame = CFrame.new(HRP.CFrame.LookVector)
end
AlignOrient.CFrame = CFrame.new(camera.CFrame.p, HRP.Position)
if BoolValues.FWD then
SpeedValue += 1 * dt * 20
SpeedValue = math.min(SpeedValue, 50)
AlignPos.Position = HRP.Position + camera.CFrame.LookVector * SpeedValue
elseif BoolValues.BWD then
AlignPos.Position = HRP.Position - camera.CFrame.LookVector * BackwardSpeedValue
elseif BoolValues.LFT then
AlignPos.Position = HRP.Position - camera.CFrame.RightVector * SideSpeedValue
elseif BoolValues.RHT then
AlignPos.Position = HRP.Position + camera.CFrame.RightVector * SideSpeedValue
end
end)
end,
[true] = function()
Flying = false
trashTable.Position:Destroy()
trashTable.Orient:Destroy()
print("no fly")
end,
}
UserInputService.InputBegan:Connect(function(input, gpe)
if gpe then return end
if input.KeyCode == Enum.KeyCode.F then
FlightChecker[Flying]()
end
if Flying then
if input.KeyCode == Enum.KeyCode.W then
BoolValues.FWD = true
end
if input.KeyCode == Enum.KeyCode.S then
BoolValues.BWD = true
end
if input.KeyCode == Enum.KeyCode.A then
BoolValues.LFT = true
end
if input.KeyCode == Enum.KeyCode.D then
BoolValues.RHT = true
end
end
end)
UserInputService.InputEnded:Connect(function(input, gpe)
if gpe then return end
if input.KeyCode == Enum.KeyCode.W then
SpeedValue = 5
BoolValues.FWD = false
end
if input.KeyCode == Enum.KeyCode.S then
BoolValues.BWD = false
end
if input.KeyCode == Enum.KeyCode.A then
BoolValues.LFT = false
end
if input.KeyCode == Enum.KeyCode.D then
BoolValues.RHT = false
end
end)