Hey there! I’m currently creating a 2.5D platformer, and it seems as all my boosts are working correctly, i’d like to improve on the diagonal boost as it seems its not moving the player far. any idea why its doing this?
Video
Code
--services
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
--player vars
local client = Players.LocalPlayer
local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local animation = script:WaitForChild("Animation")
local animator:Animator = hum:WaitForChild("Animator")
local animTrack = animator:LoadAnimation(animation)
animTrack.Priority = Enum.AnimationPriority.Action
animTrack.Looped = false
local hasLanded = true
local boosted = false
--vars
local upwardsForce = .15 --as percent of sidewards force
local boostMagnitude = 1850
local debounce = tick()
local debounceTime = .5
local boostKey = Enum.KeyCode.Q
local hotkeys = {
["W"] = Vector3.new(0,.50,0),
["A"] = Vector3.new(-1,0,0),
["S"] = Vector3.new(0,-.50,0),
["D"] = Vector3.new(1,0,0),
}
--the function to get a normalized vector3 of the boost direction
local function getUnitDirection(): Vector3
--set up the return var
local resultVec3 = Vector3.zero
local isSideways = false
local isUp = false
local issidewaysup = false
for _, input: InputObject in pairs(UserInputService:GetKeysPressed()) do
if hotkeys[input.KeyCode.Name] then
--add up the vector forces based on the pressed hotkeys (W/A/S/D)
resultVec3 += hotkeys[input.KeyCode.Name]
end
print(input.KeyCode.Name)
if (input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.D and (not isSideways) and input.KeyCode ~= Enum.KeyCode.W) then
isSideways = true
print("sidewaystrue")
end
if input.KeyCode.Name == "W" then
isUp = true
print("isup")
end
end
if resultVec3.Magnitude > 0 then
--return the normalized vector
return resultVec3.Unit, isUp, isSideways, issidewaysup
else
--return (0,0,0) if no direction vector can be normalized
return Vector3.zero, isUp, isSideways, issidewaysup
end
end
local function boost()
--apply the boost force
local direction, isUp, isSideways, issidewaysup = getUnitDirection()
local boostVector = Vector3.zero
if isUp then
boostVector = getUnitDirection() * boostMagnitude * .5
elseif isSideways then
boostVector += Vector3.FromAxis(Enum.Axis.Top) * upwardsForce * boostMagnitude + getUnitDirection() * boostMagnitude * 1.2
end
char.PrimaryPart:ApplyImpulse(boostVector)
--update the vars
boosted = true
hasLanded = false
--play the animation
animTrack:Play()
end
UserInputService.InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean)
--ignore input on UIs, chat, etc.
if gameProcessedEvent then return end
--only boost if the designated hotkey was used
if input.KeyCode == boostKey and tick() - debounce > debounceTime and not boosted then
boost()
end
end)
hum:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
if hum.FloorMaterial and not hasLanded then
animTrack:Stop()
hasLanded = true
boosted = false
debounce = tick()
end
end)