Hi!
So currently my function makes the enemy go up when hit (works very well), but I tried making it so they get knocked back but also a bit knocked up, but I can’t manage to do it.
Here’s my code
Module.Knockback = function(Character, Target, Config)
if not Character or not Target then return end
if Target.Parent == workspace:WaitForChild("NPCs") then return end
if Config.Knockback == false then return end
local Root = Character:WaitForChild("HumanoidRootPart")
local TargetRoot = Target:WaitForChild("HumanoidRootPart")
if Root and TargetRoot then
local BV = Instance.new("BodyVelocity")
BV.P = Config.Knockback_P
BV.MaxForce = Config.Knockback_MaxForce
local Direction = (Root.Position - TargetRoot.Position).Unit
BV.Velocity = -Direction * Config.Knockback_Strength
BV.Parent = TargetRoot
game:GetService("Debris"):AddItem(BV, Config.Knockback_Lifetime)
end
end
Module.Knockback = function(Character, Target, Config)
if not Character or not Target then return end
if Target.Parent == workspace:WaitForChild("NPCs") then return end
if Config.Knockback == false then return end
local Root = Character:WaitForChild("HumanoidRootPart")
local TargetRoot = Target:WaitForChild("HumanoidRootPart")
if Root and TargetRoot then
local BV = Instance.new("BodyVelocity")
BV.P = Config.Knockback_P
BV.MaxForce = Config.Knockback_MaxForce
local Direction = (Root.Position - TargetRoot.Position).Unit
BV.Velocity = (-Direction * Config.Knockback_Strength) + Vector3.new(0,Config.KnockbackHeight,0) -- here is the error
BV.Parent = TargetRoot
game:GetService("Debris"):AddItem(BV, Config.Knockback_Lifetime)
end
end
BV.MaxForce is likely not overcoming the strength of gravity. You should increase MaxForce to (gravity * mass of the object ) * Knockup scaling factor. A knockup scaling factor of 2 would make the object be accelerated upwards (increase in upwards velocity to the upwards BodyVelocity target speed) at the rate at which gravity accelerates objects downwards when falling.
You should use two LinearVelocity (BodyVelocity) parts for this, because the force of the X, Z linear knockback will be too strong compared to the Y force which needs to overcome gravity. Also note that if you do not multiply -Direction by the (1, 0, 1) vector then it will detract from the vertical velocity of the knockback on the target, whether that’s your intention or not.