Hello, basically I am trying to make a simple sleigh flying system using BodyVelocity.
Everything works fine, i've made it to fly where is camera looking. The problem is in the Free Camera feature I am trying to make.
Everything works fine if I don't hit anything while flying, but if I do then sleigh would freak out and spin in some direction. I want to make it so it would fly to the direction of the last camera LookVector.
Here's the Video showcasing the problem: Here
And here's the Code that i've wrote, it's a local script:
local ContextActionService = game:GetService("ContextActionService")
local Character = script.Parent --Since Script is in StarterCharacterScripts, our Character is already loaded and script is already placed inside of it.
local RunService = game:GetService("RunService")
local current = time() --Debounce
local BodyVelocity --Various Variables(lol)
local MainPart
local Config
local connection
local HoldingW = false --Bool Variables
local HoldingS = false
local Looping = false
local FreeCam = false
local function SpeedUp(actionName, inputState, inputObject) --Add Speed when pressing W/Holding Accelerate Button
if inputState == Enum.UserInputState.Cancel or inputState == Enum.UserInputState.End then
HoldingW = false
else
HoldingW = true
end
repeat
local Acceleration = Config.AccelerationSpeed
local Max = Config.MaxSpeed
RunService.Heartbeat:Wait()
if Acceleration.Value > Max.Value then
Acceleration.Value = 100
end
if time() - current >= 0.5 then
current = time()
if Acceleration.Value < Max.Value then
Acceleration.Value += 10
end
end
until not HoldingW
end
local function SpeedDown(actionName, inputState, inputObject) --Decrease Speed when pressing S/holding Break button
if inputState == Enum.UserInputState.End or inputState == Enum.UserInputState.Cancel then
HoldingS = false
else
HoldingS = true
end
repeat
local Acceleration = Config.AccelerationSpeed
RunService.Heartbeat:Wait()
if Acceleration.Value < 0 then
Acceleration.Value = 0
end
if time() - current >= 0.5 then
current = time()
if Acceleration.Value > 0 then
Acceleration.Value -= 15
end
end
until not HoldingS
end
local function FreeCamera(actionName, inputState, inputObject) --Free Camera (Simple code, yet very buggy)
if inputState == Enum.UserInputState.End or inputState == Enum.UserInputState.Cancel then
FreeCam = false
else
FreeCam = true
end
end
local function BindActions() --CAS actions binding
ContextActionService:BindAction("SpeedUp",SpeedUp,true,Enum.KeyCode.W)
ContextActionService:SetTitle("SpeedUp","Accelerate")
ContextActionService:SetPosition("SpeedUp",UDim2.new(0.5,0,-0.5,0))
ContextActionService:BindAction("SpeedDown",SpeedDown,true,Enum.KeyCode.S)
ContextActionService:SetTitle("SpeedDown","Break")
ContextActionService:SetPosition("SpeedDown",UDim2.new(0.5,0,0,0))
ContextActionService:BindAction("FreeCamera",FreeCamera,true,Enum.KeyCode.C)
ContextActionService:SetTitle("FreeCamera","Free Camera")
ContextActionService:SetPosition("FreeCamera",UDim2.new(-2,0,0,0))
end
local function UnBindActions() --CAS Actions unbinding
ContextActionService:UnbindAction("SpeedUp")
ContextActionService:UnbindAction("SpeedDown")
ContextActionService:UnbindAction("FreeCamera")
end
Character.Humanoid:GetPropertyChangedSignal("SeatPart"):Connect(function() --Main Part of the Script
local SeatPart = Character.Humanoid.SeatPart
if SeatPart == nil then --If Player Leaves Vehicle
UnBindActions()
Looping = false
elseif SeatPart.Name == "SleighDriver" then --If Player Enters Vehicle
BodyVelocity = SeatPart.Parent:WaitForChild("BodyVelocity")
MainPart = SeatPart.Parent
Config = MainPart.Parent.Configuration
BindActions()
Looping = true
end
connection = RunService.Heartbeat:Connect(function() --Sleigh Flying System
if not FreeCam and Looping then
MainPart.CFrame = CFrame.lookAt(MainPart.Position,MainPart.Position + workspace.CurrentCamera.CFrame.LookVector)
BodyVelocity.Velocity = MainPart.CFrame.LookVector * Config.AccelerationSpeed.Value
elseif not Looping then
connection:Disconnect()
end
end)
end)
In case if also needed, here is BodyVelocity Properties, it's being created in a Server Script:
local function AddBodyMovers() --Add Neccesary BodyMovers
local BodyVel = Instance.new("BodyVelocity")
BodyVel.MaxForce = Vector3.new(100000,100000,100000)
BodyVel.Velocity = Vector3.new(0,0,0)
BodyVel.Parent = MainPart
BodyVelocity = MainPart:FindFirstChild("BodyVelocity")
end
Any Help would be Appreciated!
