Hello! I’ve made this dash script, however, I’m currently facing one particular problem.
I want the player to dash in one direction only, using the Z and X axes of the camera instead of the root. Although it worked, when looking up, the player will also dash in that direction (Upwards). I don’t know any more alternatives I could potentially use, therefore I’m reaching out to you guys for help!
Here’s the code!
function main:forwardDash(player, z)
if dashed then return end
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild('Humanoid')
local root = character:WaitForChild('HumanoidRootPart')
local bodyVelocity = Instance.new('BodyVelocity', root)
local dashLength = tick()
bodyVelocity.MaxForce = Vector3.new(1, 1, 1) * firstForce
bodyVelocity.Name = 'dashVelocity'
if z > 0 then
while task.wait() do
bodyVelocity.Velocity = currentCamera.CFrame.LookVector * dashStrength
if tick() - dashLength >= dashTime then
bodyVelocity:Destroy()
end
if tick() - dashLength >= dashCooldown then
dashed = false
break
end
end
else
while task.wait() do
bodyVelocity.Velocity = currentCamera.CFrame.LookVector * -dashStrength
if tick() - dashLength >= dashTime then
bodyVelocity:Destroy()
end
if tick() - dashLength >= dashCooldown then
dashed = false
print(true)
break
end
end
end
end
As for this CFrames are very similar to Vector3, the only difference would be that CFrame has orientation data, hence for why it has so many methods linked to it.
Your solution of getting the closest ground-aligned vector to the look vector is a good idea and usually works quite well.
However, there is a problem if the player is looking straight up/down where the resulting dashDirection will be (0, 0, 0), or if the camera is slightly tilted while looking mostly up/down, where the dashDirection will not be aligned with the direction the player is actually facing.
In general, it is better to find the closest ground-aligned rotation first, and then use its lookvector. This is stable even in cases where the player is looking up so high that they are technically facing backwards, or when roll is involved.
local function getGroundAlignedRotation(cframe)
local _, _, _, xx, yx, zx, xy, yy, zy, xz, yz, zz = cframe:GetComponents()
local angleY = math.atan2(zx - xz, zz + xx)
return CFrame.FromEulerAnglesYXZ(0, angleY, 0)
end
local dashDirection = getGroundAlignedRotation(cframe).lookVector