I’m trying to create a script that forces the player to move forward. But the force isn’t being applied evenly, it seems as though it’s unable to apply force directly on an axis and it only moves diagonally.
The following is a LocalScript in StarterCharacterScripts:
local PlayerService = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local player = PlayerService.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local rootPart = character:WaitForChild("HumanoidRootPart")
local RUSH_DURATION = 3
local RUSH_TOKENS = 5
local RUSH_COOLDOWN = 2
local blightState = "default"
local currentTokens = 5
local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.Parent = rootPart
bodyVelocity.MaxForce = Vector3.new(0,0,0)
RunService.Heartbeat:Connect(function()
if blightState == "rushing" then
bodyVelocity.Velocity = rootPart.CFrame.LookVector * 32
end
end)
function stateCheck()
if blightState == "default" then
if currentTokens == RUSH_TOKENS then
blightState = "rushing"
Rush()
end
end
end
function Rush()
bodyVelocity.MaxForce = Vector3.new(9000,0,9000)
wait(RUSH_DURATION)
blightState = "default"
bodyVelocity.MaxForce = Vector3.new(0,0,0)
end
UserInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton2 then
stateCheck()
end
end)