Help with using raycast to make a dash system

Hello, I am trying to make a dash system that uses raycasts to detect if there’s a part/s in the player’s direction where he/she want to dash. I have written some code but it’s not working properly. Even though I am facing a part, it doesn’t say it hit a part. Here’s the code:

local function Dash(Type, IsShiftLocked)
	local DashType = "Dash"
	local MoveDirection = CardinalConvert(Humanoid.MoveDirection)
	local DashVector = Vector3.new(0, 0, 0)
	local DashDistance = NormalDash

	if Type == "Mastered" then
		DashType = "MasteredDash"
		DashDistance = MasteredDash -- Longer dash distance
	end
	
	-- Something that gets where the player wants to dash even if
	-- the player is using shift lock or the default camera
	if IsShiftLocked == true then
		local Direction = "Forward"

		local Forward = Vector3.new(0, 0, -1)
		local Backward = Vector3.new(0, 0, 1)
		local Left = Vector3.new(-1, 0, 0)
		local Right = Vector3.new(1, 0, 0)

		if MoveDirection == Backward and UserInputService:IsKeyDown(Enum.KeyCode.S) then
			Direction = "Backward"
		elseif MoveDirection == Forward and UserInputService:IsKeyDown(Enum.KeyCode.S) then
			Direction = "Backward"
		elseif MoveDirection == Left and UserInputService:IsKeyDown(Enum.KeyCode.S) then
			Direction = "Backward"
		elseif MoveDirection == Right and UserInputService:IsKeyDown(Enum.KeyCode.S) then
			Direction = "Backward"
		end

		if MoveDirection == Backward and UserInputService:IsKeyDown(Enum.KeyCode.D) then
			Direction = "Right"
		elseif MoveDirection == Forward and UserInputService:IsKeyDown(Enum.KeyCode.D) then
			Direction = "Right"
		elseif MoveDirection == Left and UserInputService:IsKeyDown(Enum.KeyCode.D) then
			Direction = "Right"
		elseif MoveDirection == Right and UserInputService:IsKeyDown(Enum.KeyCode.D) then
			Direction = "Right"
		end

		if MoveDirection == Backward and UserInputService:IsKeyDown(Enum.KeyCode.A) then
			Direction = "Left"
		elseif MoveDirection == Forward and UserInputService:IsKeyDown(Enum.KeyCode.A) then
			Direction = "Left"
		elseif MoveDirection == Left and UserInputService:IsKeyDown(Enum.KeyCode.A) then
			Direction = "Left"
		elseif MoveDirection == Right and UserInputService:IsKeyDown(Enum.KeyCode.A) then
			Direction = "Left"
		end

		if Direction == "Forward" then
			DashVector = (HumanoidRootPart.CFrame * CFrame.new(0, 0, -DashDistance)).Position
		elseif Direction == "Backward" then
			DashVector = (HumanoidRootPart.CFrame * CFrame.new(0, 0, DashDistance)).Position
		elseif Direction == "Right" then
			DashVector = (HumanoidRootPart.CFrame * CFrame.new(DashDistance, 0, 0)).Position
		elseif Direction == "Left" then
			DashVector = (HumanoidRootPart.CFrame * CFrame.new(-DashDistance, 0, 0)).Position
		end
	else
		DashVector = (HumanoidRootPart.CFrame * CFrame.new(0, 0, -DashDistance)).Position
	end
	
	-- Using this part to see where the raycast *probably* ended?
    -- I'm not sure xD
	local Part = Instance.new("Part")
	Part.Name = "Point"
	Part.Parent = workspace.Debris
	Part.Size = Vector3.new(1, 1, 1)
	Part.CanCollide = false
	Part.Anchored = true
	Part.Material = "Neon"
	Part.Color = Color3.fromRGB(255, 0, 0)

	local RayOrigin = HumanoidRootPart.Position
	local RayDirection = DashVector
	local RayParams = RaycastParams.new()
	RayParams.FilterDescendantsInstances = { HumanoidRootPart.Parent, Part }
	RayParams.FilterType = Enum.RaycastFilterType.Blacklist

	local DashRaycast = workspace:Raycast(RayOrigin, DashVector, RayParams)
	
	-- Check if raycast hits a part or not
	if DashRaycast then
		print("Raycast hit a part - ", DashRaycast.Instance)

		Part.Position = DashRaycast.Position
	else
		print("Ray cast didn't hit a part")

		Part.Position = DashVector
	end
end

I’ve been stuck in this for a few hours now, I don’t really know how to work with raycast yet xD. Any help would be much appreciated!

1 Like

u could probalby skip all of that before this part and just do:

local RayOrigin = HumanoidRootPart.Position
	local RayDirection = DashVector
	local RayParams = RaycastParams.new()
	RayParams.FilterDescendantsInstances = { HumanoidRootPart.Parent, Part }
	RayParams.FilterType = Enum.RaycastFilterType.Blacklist

	local DashRaycast = workspace:Raycast(RayOrigin, Humanoid.MoveDirection * DashDistance, RayParams)
	
	-- Check if raycast hits a part or not
	if DashRaycast then
		print("Raycast hit a part - ", DashRaycast.Instance)

		Part.Position = DashRaycast.Position
	else
		print("Ray cast didn't hit a part")

		Part.Position = DashVector
	end

The first argument takes an origin, e.g where the raycast starts and the 2nd argument takes in a vector aka direction. Humanoid.MoveDirection is our unit vector and we multiply it by a length, aka DashDistance

1 Like

It works! However, when using Shift Lock & walking backward (As you may know, the body doesn’t turn/rotate when using Shift Lock), the player still dashes forward.

That may be due to how you handle the actual dashing physics. May I see it?

Nevermind xD Everything seems to work fine now (Now works both on Shift Lock and default, and in all directions). Thanks a lot!

1 Like