Stopping Dash When near a player

Hello!, I am Saint.

Currently, I Have a prototype dash in my game, which does not stop near a NPC or player and instead will push through them if directed at them. (You can test this with the NPC’s near your spawn)

My Game Link

Obviously, This is horrible for combat and would ruin several aspects of it.

So, the question is:

How would I Stop the dash (Specifically Front Dash) When its in the direction of a player or NPC.

Game with this feature:

Would I Raycast? Check for enemy Humanoids?
This problem I cant quite wrap my head around and it has been itching me for awhile now.

Thank you for your time,

2 Likes

You could have a box, similar to a hitbox, but only for the sole purpose of seeing if other players are in a certain radius. Then you could you use get parts in part to check to see whether other characters are in that box while dashing.

My problem with this is that actually stopping the dash is going to be hard for me,

This is the function I use for my dashes.

local function BodyVelocityApply(Times,Factor,Duration)
	local BodyVelocity = Instance.new("BodyVelocity",RootPart)
	BodyVelocity.MaxForce = Vector3.new(math.huge, 0, math.huge)

	local CurrentDirection = nil

	if Left or UserInputService:IsKeyDown(Enum.KeyCode.A) then
		CurrentDirection = "Left"
	elseif Right or UserInputService:IsKeyDown(Enum.KeyCode.D) then
		CurrentDirection = "Right"
	elseif Front or UserInputService:IsKeyDown(Enum.KeyCode.W) then
		CurrentDirection = "Front"
	elseif Back or UserInputService:IsKeyDown(Enum.KeyCode.S) then
		CurrentDirection = "Back"
	end

	for i = Times, 0, Factor do
		if CurrentDirection == "Left" then
			BodyVelocity.Velocity = Character.HumanoidRootPart.CFrame.RightVector * -i
		elseif CurrentDirection == "Right" then
			BodyVelocity.Velocity = Character.HumanoidRootPart.CFrame.RightVector * i
		elseif CurrentDirection == "Back" then
			BodyVelocity.Velocity = Character.HumanoidRootPart.CFrame.LookVector * -i
		elseif CurrentDirection == "Front" then
			BodyVelocity.Velocity = Character.HumanoidRootPart.CFrame.LookVector * i
		end
		wait(Duration)
	end
	BodyVelocity:Destroy()
end

How would I Stop a Bodyvelocity Mid Dash?

Lets say this hitbox fires a bindable or event when it detects a nearby humanoid, how would I make that alter the body velocity or even completly stop it

1 Like

Maybe setting HumanoidRootPart.AssemblyLinearVelocity to Vector3.new(0,0,0)

1 Like

Hm, Im pretty sure that would instantly stop it,

Yes it is a solution but not a optimal one…

Is there a way i could Tween or smooth down the duration, actual distance traveled of the dash

You could do one of two things, you can either just automatically destroy the body velocity when that happens or lower the body velocity to something like i - 10 when a player enters the box

1 Like

Idk, the first thing that comes to mind too smoothly stop the dash is something like:

  1. Create a BodyVelocity and set it to the opposite AssemblyLinearVelocity of HumanoidRootPart
  2. Create a variable that goes times the AssemblyLinearVelocity.
  3. And then adjust the smooth variable to stop definitely the player.

like:

--Supposing you already created and configurated the new BodyVelocity:
local smooth = 50

for i = 1,50,1 do

    smooth -= 1
    newBodyVelocity.Velocity = -Character.HumanoidRootPart.AssemblyVelocity * smooth

end

(but it might not work)

Hm, I will test this thank you

If you apply two body velocitys and they are opposite, it will more then likely end up putting the humanoid into the physics state

why not add an if statement in the for loop to break out of it if you’re near a player

I am currently working on reworking the bodyvelocity application to make it easier to stop,
Once im done, Ill try this because it sounds like a pretty good idea

2 Likes

A hacky way of doing this would be to place your main dash function in a thread. You’ll also create a hitbox x studs in front of the HRP and with this you’ll constantly check if another player is detected. If detected, you’ll cancel the thread and delete the hitbox. It makes sense in my mind idk if you can see my vision

Sorry, Im not super familiar with what a “thread” is

do you mean something like task.spawn(function() ?
but this is the best idea so far

Yea exactly… A thread is what task.spwan spwans

Worked Perfectly,
Thank you very much!

1 Like

Here is the Modified code

local function BodyVelocityApply(Times,Factor,Duration)
	local BodyVelocity = Instance.new("BodyVelocity",RootPart)
	BodyVelocity.MaxForce = Vector3.new(math.huge, 0, math.huge)

	local CurrentDirection = nil

	if Left or UserInputService:IsKeyDown(Enum.KeyCode.A) then
		CurrentDirection = "Left"
	elseif Right or UserInputService:IsKeyDown(Enum.KeyCode.D) then
		CurrentDirection = "Right"
	elseif Front or UserInputService:IsKeyDown(Enum.KeyCode.W) then
		CurrentDirection = "Front"
	elseif Back or UserInputService:IsKeyDown(Enum.KeyCode.S) then
		CurrentDirection = "Back"
	end

	for i = Times, 0, Factor do
		if HumanoidClose == false then
			if CurrentDirection == "Left" then
				BodyVelocity.Velocity = Character.HumanoidRootPart.CFrame.RightVector * -i
			elseif CurrentDirection == "Right" then
				BodyVelocity.Velocity = Character.HumanoidRootPart.CFrame.RightVector * i
			elseif CurrentDirection == "Back" then
				BodyVelocity.Velocity = Character.HumanoidRootPart.CFrame.LookVector * -i
			elseif CurrentDirection == "Front" then
				BodyVelocity.Velocity = Character.HumanoidRootPart.CFrame.LookVector * i
			end
			wait(Duration)
		else
			break
		end
	end
	BodyVelocity:Destroy()
end

local function DetectHumanoids(RootPart,Humanoid,Size,Offset,Duration)
	local Hitbox = HitboxModule.CreateHitbox()
	Hitbox.Size = Size
	Hitbox.Offset = Offset
	Hitbox.CFrame = RootPart

	Hitbox.Touched:Connect(function(Part)
		local Enemy = Part.Parent
		local EnemyHumanoid = Enemy:WaitForChild("Humanoid")

		if EnemyHumanoid ~= Humanoid then
			HumanoidClose = true
			task.wait(0.3)
			HumanoidClose = false
		end

	end)

	Hitbox:Start()
	task.wait(Duration)
	Hitbox:Stop()
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.