Hi, so I have this functioning piece of code that provides knockback to another player every time they get punched by someone. It’s nice but if you’re chasing someone around and they keep jumping, you can barely knock them back. I thought of increasing the magnitude but if you stand still, it will just reach them from so far away making it look wonky.
How could I improve this to make it a better more effective and cleaner knockback system? (Maybe even if it’s possible, make it so that you can only knockback players from the front instead of all around, but that’s out of my skill level)
Any help is appreciated, thanks!
for _, target in pairs(game.Workspace:GetDescendants()) do
if target:IsA("Humanoid") and target.Parent.Name ~= player.Name then
if (target.Parent.HumanoidRootPart.Position - player.Character.HumanoidRootPart.Position).magnitude < 6 then
--print(target.Parent.Name, "was touched")
local targetRootPart = target.Parent.PrimaryPart
local playerRootPart = player.Character.PrimaryPart
local velocity = Instance.new("BodyVelocity")
velocity.MaxForce = Vector3.new(100000,100000,100000)
local angle = ((targetRootPart.Position - playerRootPart.Position) * Vector3.new(10,0,10)).Unit * 50 + Vector3.new(0,25,0)
velocity.Velocity = angle
velocity.Parent = targetRootPart
If you want to check whether or not the player is Infront of your character you can do some easy trig for this.
We will use Dot Product.
local Angle = 90 --Players vision will be 90 in this case
-- Now check
local EnemyDirection = (EnemyPosition - PlayerPosition).Unit
local ForwardDirection = PlayerCframe.LookVector
local CosTheta = ForwardDirection:Dot(EnemyDirection)
if CosTheta > math.cos(Angle/2) then
--Player is in FOV of 90 for the enemy
I would do that but I have an animation being played in the local punch script, as well as other remote events hooked up to it that needs the punch being detected. I didn’t want to do that since it would become messy in that one script so I kept everything separate.