Different Velocities from the Same Code?

Hi guys,

So I made a Dash script, here is the entire thing:

Summary

local Cam = workspace.CurrentCamera

local Tapped = false
local DoubleTapTime = 0.2
local CoolDown = 1.5

local LastTime = tick()
local LastTime2 = tick()
local LastTime3 = tick()
local LastTime4 = tick()

local DB = false


--FORWARD DASH												
UIS.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.W then
		local now = tick()
		local difference = (now - LastTime)

		if difference <= DoubleTapTime and not DB then 
			DB = true
			HRP.Velocity = Cam.CFrame.lookVector * 200
			wait(CoolDown)
			DB = false
		end
		LastTime = tick()
	elseif UIS:IsKeyDown(Enum.KeyCode.W) and input.KeyCode == Enum.KeyCode.X then
		if not DB then
			DB = true
			HRP.Velocity = Cam.CFrame.lookVector * 200
			wait(CoolDown)
			DB = false
		end
	end
end)

--BACKWARD DASH
UIS.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.S then
		local now = tick()
		local difference = (now - LastTime2)

		if difference <= DoubleTapTime and not DB then
			DB = true
			HRP.Velocity = Cam.CFrame.lookVector * -200
			wait(CoolDown)
			DB = false
		end
		LastTime2 = tick()
	elseif UIS:IsKeyDown(Enum.KeyCode.S) and input.KeyCode == Enum.KeyCode.X then
		if not DB then
			DB = true
			HRP.Velocity = Cam.CFrame.lookVector * -200
			wait(CoolDown)
			DB = false
		end
	end
end)

--LEFT DASH
UIS.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.A then
		local now = tick()
		local difference = (now - LastTime3)

		if difference <= DoubleTapTime and not DB then
			DB = true
			HRP.Velocity = Cam.CFrame.rightVector * -200
			wait(CoolDown)
			DB = false
		end
		LastTime3 = tick()
	elseif UIS:IsKeyDown(Enum.KeyCode.A) and input.KeyCode == Enum.KeyCode.X then
		if not DB then
			DB = true
			HRP.Velocity = Cam.CFrame.rightVector * -200
			wait(CoolDown)
			DB = false
		end
	end
end)

--RIGHT DASH
UIS.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.D then
		local now = tick()
		local difference = (now - LastTime4)

		if difference <= DoubleTapTime and not DB then
			DB = true
			HRP.Velocity = Cam.CFrame.rightVector * 200
			wait(CoolDown)
			DB = false
		end
		LastTime4 = tick()
	elseif UIS:IsKeyDown(Enum.KeyCode.D) and input.KeyCode == Enum.KeyCode.X then
		DB = true
		HRP.Velocity = Cam.CFrame.rightVector * 200
		wait(CoolDown)
		DB = false
	end
end)

Basically the code increases the Velocity of the player and uses Camera.CFrame.Vector do decide the direction, based on the Key the player pressed.

It all works great but since I am using Camera.Cframe, the player can sometimes dash up into the air. This would be fine if it was the same strength as the dash is on the ground, but for some reason it seems to be twice as strong or even greater.

Here is a gif of dashing on the ground:
https://gyazo.com/315b7a436d3b37a28464fb8685516c8a

Here is a gif of dashing into the air:
https://gyazo.com/c520ef1fab933c680b6b78f8a511fac4

As you can see from the two gifs, when the dash is used when the camera is looking up or down, the dash seems to be way stronger when compared to the usual dash.

1 Like

You can multiply vectors. Here’s an example.

HRP.Velocity = (Cam.CFrame.lookVector * Vector3.new(1,0,1)) * 200

This will set the Y velocity to 0 making it not possible to dash into the air. Make sure to apply it to everywhere. You can learn more about math operations with Vector3 here.

If you want the power to be the same for all directions I recommend using .unit

HRP.Velocity = (Cam.CFrame.lookVector * Vector3.new(1,0,1)).unit * 200

.unit returns a normalized copy of the vector with a magnitude of 1.

1 Like

@XxCrazysZz12 I don’t believe that’s what @PowerHouse2424 intends as he wants the dash to be of the same strength and not only dash horizontally.

I believe the issue instead has to do with how Humanoids work as when moving horizontally along the floor the Humanoid controller likes to apply a force to resist this hence the shorter distance as air drag and such as seen in this feature request.

Perhaps the solution should be to use BodyVelocity with a force set to math.huge or a high number then manually control the velocity value in order to go apply a force against this humanoid property of air friction/drag.

2 Likes

Ah, good point. I didn’t even realize it was the humanoid root part. :sweat:

2 Likes

Yes that right, that why I used Camera.LookVector so the player would be able to sub the dash in as kind of a secondary jump if they wanted too.

I’ll test with BodyVelocity and see if that fixes my issue.