View difference between Paste ID: HL1QBY0y and n0XKqQcy
SHOW: | | - or go back to the newest paste.
1
local UserInputService = game:GetService("UserInputService")
2
local player = game.Players.LocalPlayer
3
4
local DashAnimation = script:WaitForChild('Animation')
5
6
-- Settings
7
local key = Enum.KeyCode.F -- key to trigger dash
8
local velocity = 14000 -- dash speed
9
local debounce = false -- debounce, do not set to true
10
local cooldown = 2 -- cooldown after dash
11
local duration = 0.4 -- dash duration
12
--
13
14
local function Dash()
15
	local character = player.Character
16
	if character and not debounce then
17
		debounce = true
18
		-- now we begin the dash script here
19
		
20
		local humanoid = character.Humanoid
21
		local HRP = character.HumanoidRootPart -- HRP stands for HumanoidRootPart
22
		
23
		local loadedAnimation = humanoid.Animator:LoadAnimation(DashAnimation)
24
		
25
		local dashDirection = nil
26
		local moveDirection = humanoid.MoveDirection
27
		local lookVector = HRP.CFrame.LookVector
28
		local minusVelocity = -velocity -- in CFrame, if we set Z to positive, player will go move backward
29
										-- instead of forward
30
		
31
		-- checking if player is on ground, not floating
32
		local isOnGround = humanoid.FloorMaterial ~= Enum.Material.Air and humanoid.FloorMaterial ~= Enum.Material.Water
33
		
34
		if isOnGround then
35
			
36
			
37
			if moveDirection == Vector3.new(0,0,0) then -- if player is not currently moving/walking
38
				dashDirection = HRP.Position + Vector3.new(lookVector.X, 0, lookVector.Z)
39
			else -- if player are currently moving/walking
40
				dashDirection = HRP.Position + Vector3.new(moveDirection.X, 0, moveDirection.Z)
41
			end
42
			
43
			-- using bodygyro to rotate player to the dash direction smoothly
44
			local bodyGyro = Instance.new("BodyGyro")
45
			bodyGyro.Parent = HRP
46
			bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
47
			bodyGyro.D = 0 -- D is the dampening
48
			bodyGyro.P = 500000 -- P is aggressiveness
49
			bodyGyro.CFrame = CFrame.lookAt(HRP.Position, dashDirection) -- Making player look at the dash direction
50
			
51
			local attachment = Instance.new("Attachment")
52
			attachment.Parent = HRP
53
			
54
			-- now using VectorForce to move player forward
55
			local vectorForce = Instance.new("VectorForce")
56
			vectorForce.Parent = HRP
57
			-- VectorForce need attachment to tell where is player looking at
58
			vectorForce.Attachment0 = attachment
59
			vectorForce.Force = Vector3.new(0,0,minusVelocity) -- now it will move player forward as the settings
60
			
61
			loadedAnimation:Play() -- play the dash animation
62
			humanoid.AutoRotate = false -- prevent player to rotate by themselves
63
			
64
			wait(duration)
65
			
66
			humanoid.AutoRotate = true
67
			
68
			vectorForce:Destroy()
69
			bodyGyro:Destroy()
70
			attachment:Destroy()
71
			
72
			--wait(duration) -- give some time before stopping the dash animation
73
			loadedAnimation:Stop()
74
75
		end
76
		
77
		
78
		--
79
		wait(cooldown)
80
		debounce = false
81
	end
82
end
83
84
UserInputService.InputBegan:Connect(function(input)
85
	if input.KeyCode == key then
86
		Dash()
87
	end
88
end)