How do i stop BodyVelociting from flinging the player?

I used BodyVelocity for a skill, and it works great if you don’t collide with anything, but if you do…

so, how do i stop this from happening?

this is the BodyVelocity part of the code:

	local BV = Instance.new("BodyVelocity", HRP)
	BV.MaxForce = Vector3.new(math.huge, 0, math.huge)
	BV.Velocity = HRP.CFrame.LookVector * 150

	task.wait(0.07)
	BV:Destroy()

you’re giving the player near infinite force for 0.07 seconds, causing the player to fling upon hitting a wall
you should lower it down to around 500-1000.

1 Like

Adding onto what @MikeartsRBLX said, if decreasing the values doesn’t entirely give you a solution; you can try setting a raycast from the player’s head, when this ray hits an instance you stop the dash effect.

local RAY_DISTANCE = 15

local Params = RaycastParams.new()
Params.FilterDescendantsInstances = Character:GetDescendants() -- Just in case, ignore if the character triggers the ray
Params.FilterType = Enum.RaycastFilterType.Exclude

local headPosition = Character.Head.Position
local headDirection = Character.Head.CFrame.LookVector * RAY_DISTANCE

local headResult = workspace:Raycast(headPosition, headDirection, Params)

if headResult and headResult.Instance then 
-- Remove BodyVelocity
end

this works, but you would need an efficient loop for it if you want to check every wall the player hits while not potentially causing performance issues.

orr. just raycast when the dash function is fired

I would disable the falling down state of the humanoid which causes the ragdoll, it will still bounce off but will stop the fling.

Yes, that’s exactly what you’re supposed to do.

i lowered the MaxForce, used a raycast and disabled the falling down state, but i still get flinged if i hit the wall on certain angles

@owedkarma @dthecoolest @MikeartsRBLX

this is the code:

local function ApplyVelocityWithCollisionCheck(Character, Direction, Force, MaxDuration)
	-- Used ChatGPT for this because i suck with raycast
	
	local Humanoid = Character:FindFirstChild("Humanoid")
	local HRP = Character:FindFirstChild("HumanoidRootPart")
	if not Humanoid or not HRP then return end

	-- Prevent falling state
	Humanoid.PlatformStand = true

	local BV = Instance.new("BodyVelocity")
	BV.MaxForce = Vector3.new(100000, 0, 100000) -- Constrain to horizontal movement
	BV.Velocity = Direction * Force
	BV.Parent = HRP

	local RaycastParams = RaycastParams.new()
	RaycastParams.FilterDescendantsInstances = {Character} -- Ignore the character itself
	RaycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local StartTime = tick()
	local EndTime = StartTime + MaxDuration

	local RayLength = Force * MaxDuration -- Calculate the max range based on speed and duration

	while tick() < EndTime do
		local RayOrigin = HRP.Position
		local RayDirection = Direction * RayLength -- Extend the ray length dynamically

		local RayResult = workspace:Raycast(RayOrigin, RayDirection, RaycastParams)

		if RayResult then
			-- Collision detected, stop the movement and adjust position to avoid clipping
			BV:Destroy()

			-- Optional: Move the character slightly back to prevent clipping into the wall
			HRP.CFrame = CFrame.new(RayResult.Position - Direction * 1)

			-- Reset falling prevention
			Humanoid.PlatformStand = false
			return
		end

		task.wait(0.01) -- Check for collisions every frame
	end

	-- Reset falling prevention
	Humanoid.PlatformStand = false
	-- Destroy BodyVelocity after the duration
	BV:Destroy()
end

-- Here is how it gets fired
	local Direction = HRP.CFrame.LookVector -- Movement direction
	local Force = 150 -- Movement speed
	local Duration = 0.07 -- Max duration of movement

	ApplyVelocityWithCollisionCheck(Character, Direction, Force, Duration)