So I’m trying to make a gun that boosts you upwards, similar to the Barret .50 in Arsenal; and so far I’m using a :ApplyImpulse() to the player’s HumanoidRootPart, the only problem is the initial boost launches you about 20 studs, but once you’re actually in the air it will barely boost you at all. I basically need to find a way that launches the player about 20 studs upwards no matter how high they are.
Yeah goodluck trying to make a gun that boosts you upwards, similar to the Barret .50 in Arsenal.
I have the gun, all I need an improvement on the way it boosts you. Thanks for the solution btw.
You’re welcome for the solution.
Calculate your force as following :
local force = humanoidRootPart.CFrame.YVector*height
Why being so mean upfront? I don’t get it
In Roblox, when you want to apply an impulse to a player’s character to launch them a certain distance, you have to take into account various factors such as the character’s mass, gravity, and the current velocity of the character. The :ApplyImpulse()
method applies a one-time force to the center of mass of the HumanoidRootPart
, and the effect it has can vary depending on the character’s situation.
Since you are noticing that the impulse effect is different when the player is already in the air, it’s likely because there is less resistance against the character (no ground friction) and the character may already have a vertical velocity that affects how the impulse changes their motion.
To achieve a consistent impulse that propels the player upwards by approximately 20 studs regardless of their current state, you might need to adjust the impulse based on the character’s current velocity. Here’s a conceptual approach to applying such an impulse:
- Calculate the desired change in velocity (Δv) needed to reach 20 studs high, taking into account gravity.
- Before applying the impulse, measure the character’s current vertical velocity (v_current).
- Adjust the impulse to account for the current velocity so that the resultant velocity after the impulse (v_resultant) is equal to the desired Δv plus v_current.
Here’s a simplified code snippet to illustrate this:
local function launchPlayer(player, targetHeight)
local humanoidRootPart = player.Character and player.Character:FindFirstChild("HumanoidRootPart")
if not humanoidRootPart then return end
local gravity = workspace.Gravity
local mass = humanoidRootPart.AssemblyMass
-- Using the kinematic equation: Δy = v_initial * t + 0.5 * a * t^2
-- Solving for v_initial when Δy = targetHeight and a = -gravity gives:
local desiredInitialVelocity = math.sqrt(2 * gravity * targetHeight)
-- Get the current upward velocity of the HumanoidRootPart
local currentVelocityY = humanoidRootPart.Velocity.Y
-- Adjust the desired initial velocity based on current upward velocity
-- If the player is already moving upwards, we subtract that from the desired velocity
local adjustedVelocity = desiredInitialVelocity - currentVelocityY
-- Calculate the impulse required to achieve the adjusted velocity
local impulse = Vector3.new(0, adjustedVelocity * mass, 0)
-- Apply the impulse to the HumanoidRootPart
humanoidRootPart:ApplyImpulse(impulse)
end
-- Example usage:
launchPlayer(game.Players.LocalPlayer, 20)
This code snippet defines a function launchPlayer
that takes a Player
object and a target height in studs. It calculates the necessary impulse to apply to the player’s HumanoidRootPart
to achieve a jump height close to the target height. It takes into account the character’s current vertical velocity, the gravity of the workspace, and the mass of the HumanoidRootPart
.
Remember, Roblox physics can sometimes be non-deterministic due to various factors such as network latency and the physics engine’s internal approximations. Therefore, the actual height reaaweaweaweaweaweched may vary slightly each time you apply the impulse. Testing and tweaking the values in your game environment will be necessary to get the desired effect have fun!
Wow, thank you so much! I wasn’t expecting such an in-depth reply and solution, this helps a lot
Only the best, for the best
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.