How To Make Better Knockback System?

This is my current knockback system

local function knockback(character, enemyCharacter, strength, invert)
	local duration = 0.2
	
	enemyCharacter.HumanoidRootPart.CFrame = CFrame.lookAt(enemyCharacter.HumanoidRootPart.Position, character.HumanoidRootPart.Position)
	local velocity = Instance.new("LinearVelocity", character)

	velocity.VelocityConstraintMode = Enum.VelocityConstraintMode.Line
	velocity.Attachment0 = enemyCharacter.HumanoidRootPart.RootAttachment
	velocity.ForceLimitsEnabled = false
	velocity.RelativeTo = Enum.ActuatorRelativeTo.World

	local x = enemyCharacter.HumanoidRootPart.CFrame.LookVector.Unit.X
	local z = enemyCharacter.HumanoidRootPart.CFrame.LookVector.Unit.Z
	
	if invert then
		velocity.LineDirection = Vector3.new(-x, 0, -z)
	else
		velocity.LineDirection = Vector3.new(x, 0, z)
	end

	velocity.Enabled = true
	velocity.LineVelocity = strength

	game.Debris:AddItem(velocity, duration)
end

However, the enemy kinda teleports and character also teleport backward a bit.
Is there any way to make smoother knockback?

1 Like

Instead of using the line type use the vector one and it’ll be smoother. Also, you should tween down the force so it doesn’t seem too sudden

Linear velocities are glitchy like that. Bodyvelocities have the same behavior and aren’t glitchy, even thought they ARE deprecated. If you dont wanna use a deprecated item then use sonic_848s reccomendation

1 Like

There is a Roblox function that does something similar, although I’m unsure if its what you’re looking for.

Instance:ApplyImpulse(x, y, z)

How do I use vector one? Which force are you referring to? I’m unfamilar with LinearVelocity.

I was talking about the VelocityConstraintMode. You should select the vector one instead of the line and set MaxForce to math.huge. After this, you can set the vector velocity and multiply it by your strength

I only see Enum.VelocityConstraintMode. Vector, Line, or Plane, am I missing something?

Are you able to write it out? This is what I have so far.


local function knockback(character, enemyCharacter, strength, invert)
	local duration = 0.2
	
	enemyCharacter.HumanoidRootPart.CFrame = CFrame.lookAt(enemyCharacter.HumanoidRootPart.Position, character.HumanoidRootPart.Position)
	local velocity = Instance.new("LinearVelocity", character)

	velocity.VelocityConstraintMode= Enum.VelocityConstraintMode.Line
	velocity.MaxForce = math.huge
	velocity.Attachment0 = enemyCharacter.HumanoidRootPart.RootAttachment
	velocity.ForceLimitsEnabled = false
	velocity.RelativeTo = Enum.ActuatorRelativeTo.World

	local x = enemyCharacter.HumanoidRootPart.CFrame.LookVector.Unit.X
	local z = enemyCharacter.HumanoidRootPart.CFrame.LookVector.Unit.Z
	
	if invert then
		velocity.LineDirection = Vector3.new(-x, 0, -z)
	else
		velocity.LineDirection = Vector3.new(x, 0, z)
	end

	velocity.Enabled = true
	velocity.LineVelocity = strength

	game.Debris:AddItem(velocity, duration)
end

Much appreciated.

I meant like LinearVel.VelocityConstraintMode = Enum.VelocityConstraintMode.Vector.

I have an old module I made for fun that has a very smooth knockback you want it?

1 Like

Sure! It’ll help me learn a thing or two.

Here:
SmoothKnockback.lua (2.2 KB)

This module is hella old so some things won’t make sense. You’re welcome to ask questions about it :smile:

Edit: I know you’re gonna ask about the first parameter so I’ll explain it. It takes in either a player of your choice or a character model so you can do it on NPC’s. And it is a module btw

3 Likes

May you explain on how I open this file?

Right-click on workspace or just any instance and you’ll see “Insert from file”

Here:
image

When you click on it choose Script files
image

How long should the time be before destroying the linear velocity?

You have to test applying knockbacks with different strengths and times until you find the one that suits your needs. The time you input is how long it will take for the linear velocity to get destroyed

Edit: As I was reviewing my code I realized passing an EasingStyle to the function won’t do anything. Switch these lines real quick:

Before:

local Tween = game.TweenService:Create(NumberVal, TweenInfo.new(Time, Enum.EasingStyle.Circular), {Value = Strength})

After:

local Tween = game.TweenService:Create(NumberVal, TweenInfo.new(Time, KnockbackEaseStyle), {Value = Strength})
1 Like

You should name enemyCharacter to Target and character to Pusher so it’s easier to understand what’s going on. I first thought character was the one being pushed, and was confused as to why the enemy would have the velocity in them instead.

I believe the teleporting is due to network ownership, and unfortunately there is no way to solve that. However, you can use a tween on the velocity to make it slow down over time.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.