I made a dash script which uses linear velocity.
The issue is that even though ive set the y axis forces to zero it still goes up when facing a wall.
Without a wall it works as intended as there is no change in velocity in the Y direction.
Here is my code and a video of my problem.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local Debris = game:GetService("Debris")
local PlayerObjectsContainer = require(ReplicatedStorage.Source.PlayerObjectsContainer)
local Maid = require(ReplicatedStorage.Source.Dependencies.Maid)
local KEYBOARD_INPUT = Enum.KeyCode.Q
local INITIAL_SPEED = 80
local GOAL_SPEED = 16
local DASH_DURATION = 0.45
local DIRECTIONS = {
Forward = Vector3.new(0, 0, -1),
Left = Vector3.new(-1, 0, 0),
Right = Vector3.new(1, 0, 0),
Back = Vector3.new(0, 0, 1)
}
local Dash = {}
function Dash.Start()
UserInputService.InputBegan:Connect(function(input, gp)
if
input.KeyCode ~= KEYBOARD_INPUT or
gp
then
return
end
Dash._Default()
end)
end
function Dash._Default()
local character = Players.LocalPlayer.Character
local rootPart : Part = character:FindFirstChild("HumanoidRootPart")
local attach = Instance.new("Attachment")
attach.Parent = rootPart
local lv = Instance.new("LinearVelocity")
lv.Parent = attach
lv.Attachment0 = attach
lv.ForceLimitMode = Enum.ForceLimitMode.PerAxis
lv.MaxAxesForce = Vector3.new(1, 0, 1) * 140000
lv.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
local direction = Dash._GetDirection()
lv.VectorVelocity = direction * INITIAL_SPEED
local tweenInfo = TweenInfo.new(
DASH_DURATION,
Enum.EasingStyle.Linear,
Enum.EasingDirection.Out
)
local tween = TweenService:Create(lv, tweenInfo, {VectorVelocity = direction * GOAL_SPEED})
tween:Play()
--// Placeholder for when i remake that animation controller
local animator = character:FindFirstChildWhichIsA("Humanoid"):FindFirstChildWhichIsA("Animator")
local animsByDirection : {[Vector3] : Animation} = {
[DIRECTIONS.Forward] = ReplicatedStorage.Assets.Animations.FrontDash,
[DIRECTIONS.Back] = ReplicatedStorage.Assets.Animations.Backdash,
[DIRECTIONS.Right] = ReplicatedStorage.Assets.Animations.RightDash,
[DIRECTIONS.Left] = ReplicatedStorage.Assets.Animations.Leftdash,
}
local track = animator:LoadAnimation(animsByDirection[direction])
track.Looped = false
track:Play()
track:AdjustSpeed(0.85)
task.spawn(function()
tween.Completed:Wait()
lv:Destroy()
tween:Destroy()
end)
end
function Dash._GetDirection() : Vector3
if UserInputService.MouseBehavior ~= Enum.MouseBehavior.LockCenter then
return DIRECTIONS.Forward
end
if UserInputService:IsKeyDown(Enum.KeyCode.S) then
return DIRECTIONS.Back
end
if UserInputService:IsKeyDown(Enum.KeyCode.A) and UserInputService:IsKeyDown(Enum.KeyCode.D) then
return DIRECTIONS.Forward
end
if UserInputService:IsKeyDown(Enum.KeyCode.A) then
return DIRECTIONS.Left
elseif UserInputService:IsKeyDown(Enum.KeyCode.D) then
return DIRECTIONS.Right
end
return DIRECTIONS.Forward
end
return Dash
Video: