Creating a dash; BodyVelocity math or something else?

Hey everyone!

I decided to make a dash its similiar to his Dash.

So my Dash is still a concept, so dont be afraid of the animation. Just look at the sense of the Dash:

As you can see @HavocOmega the script of @Avallachi can change the direction whenever we want:

But I want make it so that when player press the Dash key it will take some seconds to let the Humanoid change his direction, then it could look like this:

> As you saw in the video above it took a bit of seconds untill the player could change his direction. I think it took 0.5 seconds.

Here again the script of him:

local Camera = workspace.CurrentCamera

local Character = script.Parent
local Root = Character.HumanoidRootPart
local Humanoid = Character.Humanoid

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local _DashCooldown = 0.5
local _DashDistance = 30
local _DashDuration = 0.25
local _DashSpeed = _DashDistance / _DashDuration

local LastDash = 0
local LastMDirection = Vector3.new()

--//	Utilities
function EaseQuarticIn(a, b, t, d)
	local c = b - a
	t /= d
	return c * t * t * t * t + a
end

function Lerp(a, b, t) -- Linear interpolation (if you prefer this)
	return a + (b - a) * t
end

--//	Functions
function OnRenderStepped()
	-- Set character orientation
	local _, CameraYOrientation, _ = Camera.CFrame:ToOrientation()
	Root.CFrame = CFrame.new(Root.Position) * CFrame.fromOrientation(0, CameraYOrientation, 0)
	
	-- Calculate speed
	local DashSpeed = EaseQuarticIn(_DashSpeed, 0, math.min(tick() - LastDash, _DashDuration), _DashDuration) -- Decelerate towards 0 speed
	
	-- Apply velocity
	local MoveDirection = Humanoid.MoveDirection == Vector3.new() and LastMDirection or Humanoid.MoveDirection
	LastMDirection = MoveDirection
	
	local DashVelocity = MoveDirection * DashSpeed
	local Velocity = Root.AssemblyLinearVelocity
	
	Root.AssemblyLinearVelocity = DashSpeed > 0 and Vector3.new(DashVelocity.X, Velocity.Y, DashVelocity.Z) or Velocity
end

function Dash()
	local t = tick()
	if t - LastDash >= _DashCooldown and Humanoid.MoveDirection ~= Vector3.new() then -- Won't dash if standing still
		LastDash = t
	end
end

function OnInputBegan(Input, Processed)
	if Processed then return end
	
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		Dash()
	end
end

--//	Events
RunService.RenderStepped:Connect(OnRenderStepped)
UserInputService.InputBegan:Connect(OnInputBegan)

Is it BodyForce I am confused?

Sorry but can somoene tell me he understood what I said?

I am worried about my explanation

you can update the velocity in a repeat loop, that’s what I’ve done

1 Like

How could I Update it Do you have any ideas?

i would make a coroutine or a spawn function before the loop that handles for how long you are gonna dash for
after you would put in a repeat loop that checks if the player is still dashing if he is then you update his velocity by the direction he is going

Can you show me I never worked with coroutine or spawn function

Hey, I know how difficult it is to get help with something as specific as your problem, so I put something together based on what you described.

local Camera = workspace.CurrentCamera

local Character = script.Parent
local Root = Character.HumanoidRootPart
local Humanoid = Character.Humanoid

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local _DashCooldown = 0.5
local _DashDistance = 30
local _DashDuration = 0.25
local _DashSpeed = _DashDistance / _DashDuration

local LastDash = 0
local LastMDirection = Vector3.new()

--//	Utilities
function EaseQuarticIn(a, b, t, d)
	local c = b - a
	t /= d
	return c * t * t * t * t + a
end

function Lerp(a, b, t) -- Linear interpolation (if you prefer this)
	return a + (b - a) * t
end

--//	Functions
function OnRenderStepped()
	-- Set character orientation
	local _, CameraYOrientation, _ = Camera.CFrame:ToOrientation()
	Root.CFrame = CFrame.new(Root.Position) * CFrame.fromOrientation(0, CameraYOrientation, 0)
	
	-- Calculate speed
	local DashSpeed = EaseQuarticIn(_DashSpeed, 0, math.min(tick() - LastDash, _DashDuration), _DashDuration) -- Decelerate towards 0 speed
	
	-- Apply velocity
	local MoveDirection = Humanoid.MoveDirection == Vector3.new() and LastMDirection or Humanoid.MoveDirection
	LastMDirection = MoveDirection
	
	local DashVelocity = MoveDirection * DashSpeed
	local Velocity = Root.AssemblyLinearVelocity
	
	Root.AssemblyLinearVelocity = DashSpeed > 0 and Vector3.new(DashVelocity.X, Velocity.Y, DashVelocity.Z) or Velocity
end

function Dash()
	local t = tick()
	if t - LastDash >= _DashCooldown and Humanoid.MoveDirection ~= Vector3.new() then -- Won't dash if standing still
		LastDash = t
	end
end

function OnInputBegan(Input, Processed)
	if Processed then return end
	
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		Dash()
	end
end

--//	Events
RunService.RenderStepped:Connect(OnRenderStepped)
UserInputService.InputBegan:Connect(OnInputBegan)

Not 100% sure if this is what you were looking for, but if you need further help or don’t understand some of it, then make sure to let me know.

2 Likes

Thanks really!!! it helpeed a lot

hey your script is wrong once you sent me. I want talk here if you don’t answer then then I must response your post as a not solution. Because I need a new solution for my problem and its the same topic

So the differences between yours and his dash is, that he cant influence his dash after a Dash.

This is what you have been created with your solution:

This is what I want:

and can you see it?
My Dash looks like that you can change the direction like you want, but in his dash he cant influence the dash he did.
What do I want is: That what you can see in his video. What yours script doesnt look like.

It ls much harder to change the direction in the solution video but in my game (your script) thats not what I have been expected

but in my script (yours script) it feels like butter when I change the direction.

What I want is : when the dash ends then I should have the possiblity to change the direction

still there???

I need somoene that can help me with this problem

You could just zero out the Humanoid walkspeed while dashing for as long as _DashDuration.

1 Like

nop still doesnt work.

its this script:

local Character = script.Parent
local Root = Character.HumanoidRootPart
local Humanoid = Character.Humanoid

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local _DashCooldown = 0.5
local _DashDistance = 30
local _DashDuration = 0.25
local _DashSpeed = _DashDistance / _DashDuration

local LastDash = 0
local LastMDirection = Vector3.new()

--//	Utilities
function EaseQuarticIn(a, b, t, d)
	local c = b - a
	t /= d
	return c * t * t * t * t + a
end

function Lerp(a, b, t) -- Linear interpolation (if you prefer this)
	return a + (b - a) * t
end

--//	Functions
function OnRenderStepped()
	-- Set character orientation
	
	-- Calculate speed
	local DashSpeed = EaseQuarticIn(_DashSpeed, 0, math.min(tick() - LastDash, _DashDuration), _DashDuration) -- Decelerate towards 0 speed
	
	-- Apply velocity
	local MoveDirection = Humanoid.MoveDirection == Vector3.new() and LastMDirection or Humanoid.MoveDirection
	LastMDirection = MoveDirection
	
	local DashVelocity = MoveDirection * DashSpeed
	local Velocity = Root.AssemblyLinearVelocity
	
	Root.AssemblyLinearVelocity = DashSpeed > 0 and Vector3.new(DashVelocity.X, Velocity.Y, DashVelocity.Z) or Velocity
end

function Dash()
	local t = tick()
	if t - LastDash >= _DashCooldown and Humanoid.MoveDirection ~= Vector3.new() then -- Won't dash if standing still
		LastDash = t
	end
end

function OnInputBegan(Input, Processed)
	if Processed then return end
	
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		Dash()
	end
end

--//	Events
RunService.RenderStepped:Connect(OnRenderStepped)
UserInputService.InputBegan:Connect(OnInputBegan)

> You can test it out for yourself if yo want. The problem I do have here is that: while I am dashing I should not could change my direction but after I dashed I can

Sorry I’m not sure what you mean. Do you want the player to move while dashing or not?

I dont want the player move while dashing

You could just change where the LastMDirection variables changes so that it’ll always go in that direction.

local Character = script.Parent
local Root = Character.HumanoidRootPart
local Humanoid = Character.Humanoid

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local _DashCooldown = 0.5
local _DashDistance = 30
local _DashDuration = 0.25
local _DashSpeed = _DashDistance / _DashDuration

local LastDash = 0
local LastMDirection = Vector3.new()

--//	Utilities
function EaseQuarticIn(a, b, t, d)
	local c = b - a
	t /= d
	return c * t * t * t * t + a
end

function Lerp(a, b, t) -- Linear interpolation (if you prefer this)
	return a + (b - a) * t
end

--//	Functions
function OnRenderStepped()
	-- Set character orientation

	-- Calculate speed
	local DashSpeed = EaseQuarticIn(_DashSpeed, 0, math.min(tick() - LastDash, _DashDuration), _DashDuration) -- Decelerate towards 0 speed

	-- Apply velocity
	local DashVelocity = LastMDirection * DashSpeed
	local Velocity = Root.AssemblyLinearVelocity

	Root.AssemblyLinearVelocity = DashSpeed > 0 and Vector3.new(DashVelocity.X, Velocity.Y, DashVelocity.Z) or Velocity
end

function Dash()
	local t = tick()
	
	local MoveDirection = Humanoid.MoveDirection == Vector3.new() and LastMDirection or Humanoid.MoveDirection
	LastMDirection = MoveDirection
	
	if t - LastDash >= _DashCooldown and Humanoid.MoveDirection ~= Vector3.new() then -- Won't dash if standing still
		LastDash = t
	end
end

function OnInputBegan(Input, Processed)
	if Processed then return end

	if Input.KeyCode == Enum.KeyCode.Q then
		Dash()
	end
end

--//	Events
RunService.RenderStepped:Connect(OnRenderStepped)
UserInputService.InputBegan:Connect(OnInputBegan)

I dont know then it doesnt look like it, looks trash:

Therefore I got an idea

I think now I got the plan how we could create it, but I dont know how I could create it.

Here in One sentence:
While I am dashing I need someseconds to change my direction.

So how can we change @Avallachi script a bit? to make it so that we can change the dash direction after some seconds.
Right now the problem in his script is that we can change the Humanoid direction when ever we want after we dashed.

The plan of me is: that when the Humanoid dashs it will take some seconds (For example 0.5 seconds) to change the direction a bit.

We can use the script above of @Avallachi. But I dont know how I could do it.

In the end it will looks like that, do you know what I mean @HavocOmega :