Character getting flinged, and ragdolled when dashing into something

I’m trying to make my dash not fling you into space
When dashing into an object, you get flinged into space and ragdolled for a second even though I turned that off.
Looked on the Developer Hub, Youtube, Other Sources

I didn’t get a satisfying answer, and I don’t know how to fix it. Some people said to use raycasts, but I don’t know how that would work with the dash. I would appreciate any help or feedback!

-- GET SERVICES -- 
local UIS = game:GetService("UserInputService")
local SS = game:GetService("SoundService")
local RS = game:GetService("ReplicatedStorage")

-- REMOTE / PLAYER AND CHARACTER / ANIMATIONS / CONSTANTS --
local DashEvent = RS.Remotes.DashEvent
local VFX = RS.VFX

local StarterCharacter = script.Parent.Parent
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local FrontDash = RS.Animations.FrontDash
local BackDash = RS.Animations.BackDash
local LeftDash = RS.Animations.LeftDash
local RightDash = RS.Animations.RightDash

local Values = Character:WaitForChild("Values")
local WalkingBool = Values:WaitForChild("Walking")
local SprintingBool = Values:WaitForChild("Sprinting")
local BlockingBool = Values:WaitForChild("Blocking")
local DashingBool = Values:WaitForChild("Dashing")
local iFramesBool = Values:WaitForChild("iFrames")

local ShiftLock = false
local debounce = false

local animation

UIS:GetPropertyChangedSignal("MouseBehavior"):Connect(function()
	if UIS.MouseBehavior == Enum.MouseBehavior.LockCenter then
		ShiftLock = true
	else
		ShiftLock = false		
	end
end)

-- DASH --
UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent or BlockingBool.Value == true then return end
	if input.KeyCode == Enum.KeyCode.Q and not debounce then
		debounce = true
		Humanoid.Jump = false
		print("Dashing")

		local LinearVelocity = Instance.new("LinearVelocity", HumanoidRootPart)
		LinearVelocity.VelocityConstraintMode = Enum.VelocityConstraintMode.Line
		LinearVelocity.MaxForce = math.huge
		LinearVelocity.LineDirection = HumanoidRootPart.CFrame.LookVector
		LinearVelocity.LineVelocity = 75
		LinearVelocity.Attachment0 = HumanoidRootPart:FindFirstChildOfClass('Attachment')
		game.Debris:AddItem(LinearVelocity, 0.1)

		-- Determine animation based on input or character state
		if UIS:IsKeyDown(Enum.KeyCode.W) or Humanoid.MoveDirection.Magnitude == 0 or Humanoid.Jump == true then
			if ShiftLock then
			LinearVelocity.LineDirection = HumanoidRootPart.CFrame.LookVector * 75
			animation = FrontDash
			else
			LinearVelocity.LineDirection = HumanoidRootPart.CFrame.LookVector * 75
			animation = FrontDash
			end
		elseif UIS:IsKeyDown(Enum.KeyCode.S) then
			if ShiftLock then
			LinearVelocity.LineDirection = HumanoidRootPart.CFrame.LookVector  * -75
			animation = BackDash
			else
			LinearVelocity.LineDirection = HumanoidRootPart.CFrame.LookVector * 75
			animation = FrontDash
			end
		elseif UIS:IsKeyDown(Enum.KeyCode.A) then
			if ShiftLock then
			LinearVelocity.LineDirection = HumanoidRootPart.CFrame.RightVector  * -75
			animation = LeftDash
			else
			LinearVelocity.LineDirection = HumanoidRootPart.CFrame.LookVector * 75
			animation = FrontDash
			end
		elseif UIS:IsKeyDown(Enum.KeyCode.D) then
			if ShiftLock then
			LinearVelocity.LineDirection = HumanoidRootPart.CFrame.RightVector  * 75
			animation = RightDash
			else
			LinearVelocity.LineDirection = HumanoidRootPart.CFrame.LookVector * 75
			animation = FrontDash
			end
		end
			
		Humanoid.Jump = true
		Humanoid:LoadAnimation(animation):Play()
		DashEvent:FireServer()
		task.wait(0.5)
		debounce = false
	end
end)

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Include
params.FilterDescendantsInstances = {workspace.Main.Maps}

-- DUST TRAIL --
DashEvent.OnClientEvent:Connect(function(Player, HumanoidRootPart)
	for i = 1,10 do
	local ray = workspace:Raycast(HumanoidRootPart.Position, Vector3.new(0,-4,0), params)
	if ray then
		local dust = VFX.Dust:Clone()
		dust.Parent = workspace.Effects
		dust.CFrame = HumanoidRootPart.CFrame
		dust.Position = ray.Position
		dust.Attachment.Dust.Color = ColorSequence.new{ColorSequenceKeypoint.new(0,ray.Instance.Color), ColorSequenceKeypoint.new(1, ray.Instance.Color)}
		dust.Attachment.Dust:Emit(4)
		game.Debris:AddItem(dust,2)
	end
	task.wait()
	end
end)
1 Like

Hey, How other people mentioned using raycasts, which is a good idea.
First, you need to cast a ray before the player dashes to check if there’s an object in the path of the dash. If the ray hits an object, you can calculate the distance from the player to the object and set the dash distance to this calculated distance. This way, the player will stop dashing when it hits the object, preventing it from being flung into space. You might want to try this code:

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent or BlockingBool.Value == true then return end
	if input.KeyCode == Enum.KeyCode.Q and not debounce then
		debounce = true
		Humanoid.Jump = false
		print("Dashing")

		local RaycastParams = RaycastParams.new()
		RaycastParams.FilterType = Enum.RaycastFilterType.Blacklist
		RaycastParams.FilterDescendantsInstances = {Character}
		local ray = workspace:Raycast(HumanoidRootPart.Position, HumanoidRootPart.CFrame.LookVector * 75, RaycastParams)

		local dashDistance = 75
		if ray then
			dashDistance = (HumanoidRootPart.Position - ray.Position).Magnitude
		end

		local LinearVelocity = Instance.new("LinearVelocity", HumanoidRootPart)
		LinearVelocity.VelocityConstraintMode = Enum.VelocityConstraintMode.Line
		LinearVelocity.MaxForce = math.huge
		LinearVelocity.LineDirection = HumanoidRootPart.CFrame.LookVector
		LinearVelocity.LineVelocity = dashDistance
		LinearVelocity.Attachment0 = HumanoidRootPart:FindFirstChildOfClass('Attachment')
		game.Debris:AddItem(LinearVelocity, 0.1)
	end
end)

This should probably fix your issue. If it doesn’t fix the issue, please let me know.

Thanks for your response! I implemented your suggestion, and I can see how it would work, but sadly it comes up with a weird answer. I either implemented it incorrectly, or there’s a different way I should use the raycast.

Here’s the new problem
When dashing regularly it works fine, but when dashing into the object. The dash becomes really slow
and after you try dashing into the object, whenever you try to dash again it’s the same speed.

-- GET SERVICES -- 
local UIS = game:GetService("UserInputService")
local SS = game:GetService("SoundService")
local RS = game:GetService("ReplicatedStorage")

-- REMOTE / PLAYER AND CHARACTER / ANIMATIONS / CONSTANTS --
local DashEvent = RS.Remotes.DashEvent
local VFX = RS.VFX

local StarterCharacter = script.Parent.Parent
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local FrontDash = RS.Animations.FrontDash
local BackDash = RS.Animations.BackDash
local LeftDash = RS.Animations.LeftDash
local RightDash = RS.Animations.RightDash

local Values = Character:WaitForChild("Values")
local WalkingBool = Values:WaitForChild("Walking")
local SprintingBool = Values:WaitForChild("Sprinting")
local BlockingBool = Values:WaitForChild("Blocking")
local DashingBool = Values:WaitForChild("Dashing")
local iFramesBool = Values:WaitForChild("iFrames")

local ShiftLock = false
local debounce = false

local DashDistance = 75

local animation

UIS:GetPropertyChangedSignal("MouseBehavior"):Connect(function()
    if UIS.MouseBehavior == Enum.MouseBehavior.LockCenter then
        ShiftLock = true
    else
        ShiftLock = false        
    end
end)

-- DASH --
UIS.InputBegan:Connect(function(input, gameProcessedEvent)
    if gameProcessedEvent or BlockingBool.Value == true then return end
    if input.KeyCode == Enum.KeyCode.Q and not debounce then
        debounce = true
        Humanoid.Jump = false
        print("Dashing")

        local RaycastParams = RaycastParams.new()
        RaycastParams.FilterType = Enum.RaycastFilterType.Exclude
        RaycastParams.FilterDescendantsInstances = {Character}
        local ray = workspace:Raycast(HumanoidRootPart.Position, HumanoidRootPart.CFrame.LookVector * 75, RaycastParams)
        
        if ray then
            DashDistance = (HumanoidRootPart.Position - ray.Position).Magnitude
        end

        local LinearVelocity = Instance.new("LinearVelocity", HumanoidRootPart)
        LinearVelocity.VelocityConstraintMode = Enum.VelocityConstraintMode.Line
        LinearVelocity.MaxForce = math.huge
        LinearVelocity.LineDirection = HumanoidRootPart.CFrame.LookVector
        LinearVelocity.LineVelocity = DashDistance
        LinearVelocity.Attachment0 = HumanoidRootPart:FindFirstChildOfClass('Attachment')
        game.Debris:AddItem(LinearVelocity, 0.1)

        -- Determine animation based on input or character state
        if UIS:IsKeyDown(Enum.KeyCode.W) or Humanoid.MoveDirection.Magnitude == 0 or Humanoid.Jump == true then
            if ShiftLock then
                LinearVelocity.LineDirection = HumanoidRootPart.CFrame.LookVector * DashDistance
                animation = FrontDash
            else
                LinearVelocity.LineDirection = HumanoidRootPart.CFrame.LookVector * DashDistance
                animation = FrontDash
            end
        elseif UIS:IsKeyDown(Enum.KeyCode.S) then
            if ShiftLock then
                LinearVelocity.LineDirection = HumanoidRootPart.CFrame.LookVector  * -DashDistance
                animation = BackDash
            else
                LinearVelocity.LineDirection = HumanoidRootPart.CFrame.LookVector * DashDistance
                animation = FrontDash
            end
        elseif UIS:IsKeyDown(Enum.KeyCode.A) then
            if ShiftLock then
                LinearVelocity.LineDirection = HumanoidRootPart.CFrame.RightVector  * -DashDistance
                animation = LeftDash
            else
                LinearVelocity.LineDirection = HumanoidRootPart.CFrame.LookVector * DashDistance
                animation = FrontDash
            end
        elseif UIS:IsKeyDown(Enum.KeyCode.D) then
            if ShiftLock then
                LinearVelocity.LineDirection = HumanoidRootPart.CFrame.RightVector  * DashDistance
                animation = RightDash
            else
                LinearVelocity.LineDirection = HumanoidRootPart.CFrame.LookVector * DashDistance
                animation = FrontDash
            end
        end

        Humanoid.Jump = true
        Humanoid:LoadAnimation(animation):Play()
        DashEvent:FireServer()
        task.wait(0.5)
        debounce = false
    end
end)

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Include
params.FilterDescendantsInstances = {workspace.Main.Maps}

-- DUST TRAIL --
DashEvent.OnClientEvent:Connect(function(Player, HumanoidRootPart)
    for i = 1,10 do
        local ray = workspace:Raycast(HumanoidRootPart.Position, Vector3.new(0,-4,0), params)
        if ray then
            local dust = VFX.Dust:Clone()
            dust.Parent = workspace.Effects
            dust.CFrame = HumanoidRootPart.CFrame
            dust.Position = ray.Position
            dust.Attachment.Dust.Color = ColorSequence.new{ColorSequenceKeypoint.new(0,ray.Instance.Color), ColorSequenceKeypoint.new(1, ray.Instance.Color)}
            dust.Attachment.Dust:Emit(4)
            game.Debris:AddItem(dust,2)
        end
        task.wait()
    end
end)

Sorry for the long response time, Geforce Experience and Gyazo aren’t working for some reason!
I would send a video so you could have more context, but I don’t know why it’s not working.:pensive:

2 Likes