How do you make a player velocity where you “charge” whats infront of you?

Hi everyone, I wanna know how to make a script where when you hold a gear and click, it charges infront of you at a rapid speed without letting the player control during the charge. How would I be able to do that?

Here a gif of what I want underneath:

(I used walkspeed to demonstrate what I want using velocity instead of walkspeed.)

I was thinking of using velocity but it felt complicated to script it with where the angle of the user is at, etc…

Anyone can help?

2 Likes

Try this out:

local PS = game:GetService("Players")

local player = PS.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local HRP: BasePart = char:FindFirstChild("HumanoidRootPart")

HRP.AssemblyLinearVelocity *= (HRP.CFrame.LookVector * 10)
2 Likes

Not sure if it’s working. Iv edited it so it finds the character but it brings me up this error.

This is the edited code:


local character = script.Parent.Parent
local HRP: BasePart = character:FindFirstChild("HumanoidRootPart")

script.Parent.Activated:Connect(function()
HRP.AssemblyLinearVelocity *= (HRP.CFrame.LookVector * 10)
end)

1 Like

You can use the humanoid’s lookvector to get the correct velocity like downrest posted, also the reason why your script is erroring at line 5 is because

local HRP: BasePart = character:FindFirstChild("HumanoidRootPart")

should be

local HRP = character:FindFirstChild("HumanoidRootPart")

Try this for your charge

local debounce = false
local velocity = 100
local chargetime = 1
local plyr = game.Players.LocalPlayer
script.Parent.Activated:Connect(function()
	if not debounce and plyr.Character then
		debounce = true
		local character = plyr.Character
		local oldws = character.Humanoid.WalkSpeed
		character.Humanoid.WalkSpeed = 0
		local timer = tick()
		repeat
			character.HumanoidRootPart.AssemblyLinearVelocity = character.HumanoidRootPart.CFrame.LookVector*velocity
			task.wait()
		until tick() - chargetime >= timer or not plyr.Character
		character.Humanoid.WalkSpeed = oldws
		debounce = false
	end
end)

The only issue is that shiftlock or going first person will allow you to change the trajectory of the charge, so you would either have to disable shiftlock or save the initial direction of the charge as a variable before the repeat loop starts.

2 Likes

Adding on, you can do
Humanoid.AutoRotate = false
to prevent this.

2 Likes

I already made a seperate line which works the same way, thanks!

1 Like