Dashing using the camera

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
1 Like

You could just completely remove the Y axis from the CFrame

How could I do that? I’m awfully bad at CFrame’s ;-;

Something on the lines of this i believe

local dashDirection = currentCamera.CFrame.LookVector
dashDirection = Vector3.new(dashDirection.X, 0, dashDirection.Z)
bodyVelocity.Velocity = dashDirection.Unit * -dashStrength
2 Likes

long time no see :)) It is in fact unachored

We meet again!!! haha
3.0.c.h.a.r.s

1 Like

Holy crap it worked, I didn’t know it would be that easy. Thanks again mwamwa

1 Like

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.

1 Like

Oh, that makes sense then. But I’m completely confused about the use of the Unit you’ve provided in the code. I’m confused about Unit entirely.

I believe this could help you understand CFrames a bit better: How to think about CFrames

1 Like

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
1 Like