Make Player Dash Towards Cursor?

I’m currently working on a 2D Platformer and I’m trying to allow the player to dash toward the mouse cursor. Does anyone have any ideas? Any help is appreciated :smile:

What I’ve currently got:

local Debris = game:GetService('Debris')
local UIS = game:GetService('UserInputService')
local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")


local Player = game:GetService('Players').LocalPlayer
local Character = Player.Character
local HRP = Character:WaitForChild("HumanoidRootPart")
local Cooldown = false


UIS.InputBegan:Connect(function(Input, GameProccessed)
	if not GameProccessed and UIS:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) and Cooldown == false then
		Cooldown = true
		local Bv = Instance.new("BodyVelocity")
		Bv.Parent = Character.UpperTorso
		Bv.P = 1250
		Bv.MaxForce = Vector3.new(math.huge,math.huge, 0)
		Bv.Velocity = Character.UpperTorso.CFrame.lookVector*50
		Debris:AddItem(Bv, 0.2)
		task.wait(2)
		Cooldown = false
	end
end)

Simply just launches the player towards the direction that the UpperTorso is facing.

For Those who are attempting to do something similar to this, Here’s what I ended up coming up with.

local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")
local Player = game:GetService('Players').LocalPlayer
local Mouse = Player:GetMouse()
local Character = Player.Character
local HRP = Character:WaitForChild("HumanoidRootPart")

local Cooldown = false

function getMass(model)
	if model and model:IsA("Model") then
	local mass = 0;
		for i,v in pairs(model:GetDescendants()) do
			if(v:IsA("BasePart")) then
				mass += v.AssemblyMass;
			end
		end
		return mass;
	end
end

UIS.InputBegan:Connect(function(Input, GameProccessed)
	if not GameProccessed and UIS:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) and Cooldown == false then
		Cooldown = true
		local direction = CFrame.new(HRP.Position, Mouse.Hit.Position).LookVector
		HRP:ApplyImpulse(direction * getMass(Character) * 2)
		task.wait(2)
		Cooldown = false
	end
end)
1 Like