Make player go foward

Hello. I am trying to make a thing where if i press shift and x it launches me forward. I have a script but instead of launching me forward it launches me back. Heres an example of a game that I play that has it:

as you can see the player launches forward and a little bit up. I want it to be the same. here is my code:

local Player = game.Players.LocalPlayer
local character = Player.Character or Player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

Player.CharacterAdded:Connect(function(char)
	character = char
	humanoid = character:WaitForChild("Humanoid")
end)

local function getModelMass(model: Model): number
	local mass = 0

	for _, v in ipairs(model:GetDescendants()) do
		if v:IsA('BasePart') then
			mass += v:GetMass()
		end
	end

	return mass
end

-- force = mass * acceleration
local fowardForce = getModelMass(character) * 85

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	if (input.KeyCode == Enum.KeyCode.LeftShift and UIS:IsKeyDown(Enum.KeyCode.X)) or (input.KeyCode == Enum.KeyCode.X and UIS:IsKeyDown(Enum.KeyCode.LeftShift)) then
		print("Wow")
		humanoid.PlatformStand = true
		character.HumanoidRootPart:ApplyImpulse(-character.HumanoidRootPart.CFrame.LookVector.Unit * fowardForce)

		wait(5)
		humanoid.PlatformStand = false
	end
end)

this is an example of what I’m trying to achieve. Not just going forward but a little bit up:

1 Like

You have a slight issue in your code on the line where the impulse is applied. You’re automatically making it minus, also I don’t think you need to turn it into a Unit vector since LookVector is already a unit. Here’s your code:

                                --   < HERE > --
character.HumanoidRootPart:ApplyImpulse(-character.HumanoidRootPart.CFrame.LookVector.Unit 
fowardForce)

How it should be:

character.HumanoidRootPart:ApplyImpulse(character.HumanoidRootPart.CFrame.LookVector * 
fowardForce)

If it doesn’t work, my bad, but try to experiment with this to your liking and hopefully it works out for you! :grinning_face_with_smiling_eyes:

1 Like

Oh! My bad. I didn’t see my error. Also! It worked. But I noticed it makes the player launch just forward. is it possible to make it launcher the player forward but also slight up?

Yes! Simply apply an upward velocity, if you want to do it using the UpVector instead of LookVector, or you could simply use:

   Vector3.new(0,10,0)

etc etc, but it’s really up to you! You’d just have to add this on to the look vector and you’re in business.

1 Like

Question At what place in LookVector do I add the Vector3.new(0,10,0)?

You simply add it on using the + operator.
Again:

character.HumanoidRootPart:ApplyImpulse(character.HumanoidRootPart.CFrame.LookVector * 
fowardForce + Vector3.new(0,10,0))

OR:

character.HumanoidRootPart:ApplyImpulse(character.HumanoidRootPart.CFrame.LookVector * 
fowardForce + character.HumanoidRootPart.UpVector * 10) -- You can change this however you wish.

Both options are viable.

1 Like

Strange. I tried the top option but it still does not launch me slightly up. It still launches me foward

Would you mind showing me the code?

local Player = game.Players.LocalPlayer
local character = Player.Character or Player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

Player.CharacterAdded:Connect(function(char)
	character = char
	humanoid = character:WaitForChild("Humanoid")
end)

local function getModelMass(model: Model): number
	local mass = 0

	for _, v in ipairs(model:GetDescendants()) do
		if v:IsA('BasePart') then
			mass += v:GetMass()
		end
	end

	return mass
end

-- force = mass * acceleration
local fowardForce = getModelMass(character) * 85

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	if (input.KeyCode == Enum.KeyCode.LeftShift and UIS:IsKeyDown(Enum.KeyCode.X)) or (input.KeyCode == Enum.KeyCode.X and UIS:IsKeyDown(Enum.KeyCode.LeftShift)) then
		print("Wow")
		humanoid.PlatformStand = true
		character.HumanoidRootPart:ApplyImpulse(character.HumanoidRootPart.CFrame.LookVector * 
			fowardForce + Vector3.new(0,10,0))

		wait(5)
		humanoid.PlatformStand = false
	end
end)

Strange indeed. Try the bottom option too, hopefully that’ll make a difference.

UpVector is not a valid member of Part “Workspace.ForgottenDogYT.HumanoidRootPart”

It actually does launch you up, it’s just that the force is so small that it’s not noticable. You also need to take your character’s mass into account like you did with the forward force

local mass = getModelMass(character)
local fowardForce = mass * 85
local upForce = mass * 40

character.HumanoidRootPart:ApplyImpulse(character.HumanoidRootPart.CFrame.LookVector * 
			fowardForce + Vector3.new(0,upForce,0))
1 Like

so how would it look like since?

Replace

local fowardForce = getModelMass(character) * 85

with

local mass = getModelMass(character)
local fowardForce = mass * 85
local upForce = mass * 40

and replace the line where you apply your impulse with

character.HumanoidRootPart:ApplyImpulse(character.HumanoidRootPart.CFrame.LookVector * 
			fowardForce + Vector3.new(0,upForce,0))
1 Like

Oh now it launches foward and up! Thank you

1 Like