Help with BodyForce?

I’m making SSB esque lava system that increases a damage value when touched and then launches you up and back the opposite direction you are facing. My problem is that I’m not sure how to apply more than one direction to this and even then this doesn’t seem to do anything.

local Character = Player.Character
local Lava = workspace.Lava
local Damage = Instance.new("NumberValue")
local BaseDamage = 0
local cooldown = false
Damage.Name = "Damage"
Damage.Parent = Player



Lava.Touched:Connect(function(hit)
	if cooldown == false then
		Damage.Value = Damage.Value + math.random(1,4)
		local HumanoidRootPart = Character.HumanoidRootPart
		local BodyForce = Instance.new("BodyForce", HumanoidRootPart)
		BodyForce.Force = -(HumanoidRootPart.CFrame.LookVector) * Damage.Value
		cooldown = true
		wait(1) -- Delay
		cooldown = false

		BodyForce:Destroy()  
	end
	end)

Try using BasePart:ApplyImpulse on the humanoid root part.

Force physics formula is: Force = Mass * Acceleration
For the direction simply add the upVector of the HRP and the opposite of the lookVector.

Something like this:

local function LavaLaunch(character)
	local cf = character:GetPivot()
	local dir = cf.UpVector + (-cf.LookVector)
	local accel = game.workspace.gravity + 50 --Adjust this value for different height/distance
	
	local mass = 0
	local child = character:GetChildren()
	for i = 1, #child do
		if(child[i]:IsA("BasePart"))then
			mass += child[i].Mass
		end
	end
	
	local force = dir * mass * accel
	character.HumanoidRootPart:ApplyImpulse(force)
	
end
1 Like

That’s a good idea! Though how would I increase the force depending on how high the damage value is? And do this when a player touches the lava?

I would set a maximum possible damage, then get a percentage of the damage being done. Then you can take a percentage of the maximum launch acceleration using the damage percentage. Higher damage = higher launch. Something like this:

local Character = Player.Character
local Lava = workspace.Lava
local Damage = Instance.new("NumberValue")
local BaseDamage = 0
local cooldown = false
Damage.Name = "Damage"
Damage.Parent = Player

local MaxDamage = 50 --Set to maximum possible damage
local MaxAccel = 50 --Adjust this to increase/decrease max force acceleration

--Get the total mass of a model
local function GetModelMass(model: Model)
	local mass = 0
	local child = model:GetChildren()
	for i = 1, #child do
		if(child[i]:IsA("BasePart"))then
			mass += child[i].Mass
		end
	end
	
	return mass
end

--Launch humanoid character up and backward based on damage amount
local function LavaLaunch(character: Model, dmg: Number)
	local cf = character:GetPivot() --Get Character Model CFrame
	local dir = cf.UpVector + (-cf.LookVector) --Set direction of launch (Up + Back)
	
	local dmgPer = math.min(dmg, MaxDamage)/MaxDamage --percentage of maximum damage
	local accel = game.workspace.gravity + (MaxAccel*dmgPer) --Adjust this value for different height/distance
	
	local mass = GetModelMass(character)

	local force = dir * mass * accel
	character.HumanoidRootPart:ApplyImpulse(force)
end

--Upon touching the lava, increase damage done and perform launch.
Lava.Touched:Connect(function(hit)
	if(cooldown)then return end
	cooldown = true
	
	local increasedDmg = Damage.Value + math.random(4)
	local dmg = math.min(MaxDamage, increasedDmg)
	LavaLaunch(Character, dmg)
	
	task.wait(1)
	cooldown = false
end)
1 Like

This works somewhat, the only problem being the damage value itself doesn’t go up and it seems to push me the same distance each time but I think i can figure it out from here