Dash not working the way I want it

  1. I want to achieve a dash system which is used simply to dash, Forward, Backward, Left and Right

  2. I thought everything was working good until I realized it goes wrong when I dont use shift-lock

Using Shift-Lock everything works good since it goes to the right left front etc of the player.

Without using shiftlock the axis works but I dont want it to be like that. I want it to still dash left or right even if the character is facing that way. Here is The Problem I have :


as you can see the dash torwards the left goes backwards because the left isnt the same since the character is rotated. I would like to know how to fix this. Thank you very much.

I have tried using MoveDirection but I am not familiar with it and was unsuccessful.

Here is the script :

dashRemote.OnServerEvent:Connect(function(plr, key)
	print(plr.Name.." is dashing")
	print(key)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local humr = char.HumanoidRootPart
	local BP = Instance.new("BodyPosition")
	BP.Parent = humr
	BP.MaxForce = Vector3.new(1000000,1000000,1000000)
	if key == "W" then
			BP.Position = (humr.CFrame * CFrame.new(0,0,-60)).Position
	elseif key == "A" then
		BP.Position = (humr.CFrame * CFrame.new(-60,0,0)).Position
	elseif key == "S" then
		BP.Position = (humr.CFrame * CFrame.new(0,0,60)).Position
	elseif key == "D" then
		BP.Position = (humr.CFrame * CFrame.new(60,0,0)).Position
	end
	
	local anim = Instance.new("Animation")
	anim.AnimationId = "rbxassetid://8000617431"
	local dashAnim = char.Humanoid:LoadAnimation(anim)
	dashAnim:Play()
	wait(0.1)
	BP:Destroy()
	wait(0.2)
	dashAnim:Stop()
	anim:Destroy()
end)
1 Like

Get the player’s camera’s LookVector, and get the X and Z values. (We don’t want the Y value as you noticed already in your code.) You probably also need to get the .Unit vector since we are loosing the Y component here.

Then, you’ll want to multiply? that vector with your existing CFrame manipulation so that it’s relative to your player’s camera. Hope this gets you on the right track and unstuck!

3 Likes

Sorry for the late reply, yeah I will try that out. I know about the LookVector but I really don’t know anything about .Unit. I’m gonna search it up and see how I can implement it in my code.

1 Like

Ok great! .Unit is a pretty simple. A vector has two pieces of information: magnitude and direction. What .Unit does is takes in a vector and makes a new vector with a magnitude of 1, while preserving its direction.

In your case, we want the direction of the player’s camera, but not the magnitude of the player’s camera. If we included the magnitude of the player’s camera, then the camera angle would influence how far we dashed, which is not what we want. We want the dash to be the same distance (magnitude) every time, but in a different direction based on the camera angle.

To calculate the magnitude of a 3D vector, you take the square root of (xx + yy + zz). Note that xx is the same as x^2, but x*x is a little more clear in text notation.

Here’s an easy example. Let’s say you have a LookVector of <0.5, 0.707, 0.5>. If you use the equation above, you will see that vector has a magnitude of 1.

Now, remember that we don’t want the y value in your case. So we change the y value to 0. Now we have <0.5, 0, 0.5>. Now, our magnitude is 0.707, which is not a magnitude of 1! So we can use the .Unit function on that vector, and we would get a new vector of <0.707, 0, 0.707>, which now has a magnitude of 1 again.

So, using .Unit after removing the vector’s y component lets you preserve the dash’s direction, while not influencing the distance (magnitude) of the dash!

The syntax is pretty simple in the code. Example:

local camera_vector = Vector3.new(0.5, 0, 0.5)
local new_camera_vector = camera_vector.Unit
print(new_camera_vector)
-- Should print out (0.707, 0, 0.707)
1 Like

ohh I see what you mean. Your edit answered most of my questions, but how exactly do I get the LookVector value and implement it in my script since it is mostly used by CFrame. Isnt LookVector like a Vector3 value or something? I’m just confused since when I try to include it in my script it gives things like “Position is not a valid member of Vector3”.

Im not sure how to remove the Y value of the lookVector either. :thinking:
I’ll do some research about that and I’ll let you know if I find anything. I guess I really need to learn more about CFrame and Vector3 :joy:

I did this and changed it up but unfortunately it is doing the exact same thing. I think I am doing something wrong but I am not really sure what to do at this point. :thinking:

dashRemote.OnServerEvent:Connect(function(plr, key, lookVector)
	print(plr.Name.." is dashing")
	print(key)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local humr = char.HumanoidRootPart
	local BP = Instance.new("BodyPosition")
	BP.Parent = humr
	BP.MaxForce = Vector3.new(1000000,1000000,1000000)
	if key == "W" then
		BP.Position = (humr.CFrame * CFrame.new(0,0,-60) * lookVector.Unit)
	elseif key == "A" then
		BP.Position = (humr.CFrame * CFrame.new(-60,0,0) * lookVector.Unit)

using the camera’s cframe for velocity instead of the humanoidrootpart’s seem to work fine

local player = game.Players.LocalPlayer
local Char = player.Character
local UserInputService = game:GetService("UserInputService")

local camera = workspace.Camera

local debounce = true

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E and debounce then
		debounce = false
		local HumRP = Char.HumanoidRootPart
		HumRP.Velocity = camera.CFrame.lookVector * 200
		wait(1.5) -- cooldown
		debounce = true
	end
end)



UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.R and debounce then
		debounce = false
		local HumRP = Char.HumanoidRootPart
		HumRP.Velocity = camera.CFrame.rightVector * 200
		wait(1.5) -- cooldown
		debounce = true
	end
end)
2 Likes

I know this is a very old thread, but, How would you do this but dashing backwards? When I try " -Camera.CFrame.lookVector * 200" it sends me backwards but up in the air aswell.