In order to achieve this, you will need to calculate a vector that points in the direction the attack is coming from. Assuming that c is the attacker’s character, you can go about calculating the direction vector like this:
local direction = (enemyChar.HumanoidRootPart.Position - c.HumanoidRootPart.Position).Unit
Then use this vector to calculate the PlaneVelocity for the LinearVelocity object:
The MaxForce is pretty high. I would try adjusting the MaxForce and see how much of a difference that makes. Keep it no more than 10,000 to test. If it’s too little, I would maybe keep it less than 25,000
-- assuming c is the attacker's character
local vec = c.HumanoidRootPart.CFrame.LookVector
local knockbackSpeed = 120 -- adjust as needed
local knockbackDirection = Vector2.new(vec.X, vec.Z).Unit -- normalize the vector
local linear = Instance.new("LinearVelocity")
linear.Enabled = true
linear.VelocityConstraintMode = Enum.VelocityConstraintMode.Plane
local att = Instance.new("Attachment")
att.CFrame = enemyChar.HumanoidRootPart.CFrame
att.Parent = enemyChar.HumanoidRootPart
linear.Attachment0 = att
linear.MaxForce = 60000
linear.PrimaryTangentAxis = knockbackDirection
linear.SecondaryTangentAxis = Vector3.new(0, 1, 0) -- set to up vector
linear.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
linear.PlaneVelocity = knockbackDirection * knockbackSpeed
linear.Parent = enemyChar
-- assuming c is the attacker's character
local vec = c.HumanoidRootPart.CFrame.LookVector
local knockbackSpeed = 120 -- adjust as needed
local knockbackDirection = Vector3.new(vec.X, 0, vec.Z).Unit -- convert to Vector3 and set Z component to 0
local linear = Instance.new("LinearVelocity")
linear.Enabled = true
linear.VelocityConstraintMode = Enum.VelocityConstraintMode.Plane
local att = Instance.new("Attachment")
att.CFrame = enemyChar.HumanoidRootPart.CFrame
att.Parent = enemyChar.HumanoidRootPart
linear.Attachment0 = att
linear.MaxForce = 60000
linear.PrimaryTangentAxis = knockbackDirection
linear.SecondaryTangentAxis = Vector3.new(0, 1, 0) -- set to up vector
linear.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
linear.PlaneVelocity = knockbackDirection * knockbackSpeed
linear.Parent = enemyChar
Chileee that ain’t a solution! What I gave you wasn’t 100% the answer because what you described didn’t really map out in my head, can you please describe what you wish to achieve again? x
This is the best I could do, I tried my best modifying the code! If there are errors please let me know.
-- assuming c is the attacker's character
local vec = c.HumanoidRootPart.CFrame.LookVector
local knockbackDistance = 10 -- adjust as needed
local knockbackSpeed = 120 -- adjust as needed
local knockbackDirection = Vector3.new(vec.X, 0, vec.Z).Unit -- convert to Vector3 and set Z component to 0
local knockbackVector = knockbackDirection * knockbackDistance -- calculate the knockback vector
local linear = Instance.new("LinearVelocity")
linear.Enabled = true
linear.VelocityConstraintMode = Enum.VelocityConstraintMode.Plane
local att = Instance.new("Attachment")
att.CFrame = enemyChar.HumanoidRootPart.CFrame
att.Parent = enemyChar.HumanoidRootPart
linear.Attachment0 = att
linear.MaxForce = 60000
linear.PrimaryTangentAxis = knockbackDirection
linear.SecondaryTangentAxis = Vector3.new(0, 1, 0) -- set to up vector
linear.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
linear.PlaneVelocity = knockbackVector / (1 / knockbackSpeed) -- calculate the knockback velocity
linear.Parent = enemyChar
Omigoshh I made the same mistake chile it occurred because the PlaneVelocity property of LinearVelocity requires a 2D vector and not a 3D vector, I converted the knockbackVector to Vector2 before assigning it to PlaneVelocity, I hope this works.
-- assuming c is the attacker's character
local vec = c.HumanoidRootPart.CFrame.LookVector
local knockbackDistance = 10 -- adjust as needed
local knockbackSpeed = 120 -- adjust as needed
local knockbackDirection = Vector3.new(vec.X, 0, vec.Z).Unit -- convert to Vector3 and set Z component to 0
local knockbackVector = knockbackDirection * knockbackDistance -- calculate the knockback vector
local linear = Instance.new("LinearVelocity")
linear.Enabled = true
linear.VelocityConstraintMode = Enum.VelocityConstraintMode.Plane
local att = Instance.new("Attachment")
att.CFrame = enemyChar.HumanoidRootPart.CFrame
att.Parent = enemyChar.HumanoidRootPart
linear.Attachment0 = att
linear.MaxForce = 60000
linear.PrimaryTangentAxis = knockbackDirection
linear.SecondaryTangentAxis = Vector3.new(0, 1, 0) -- set to up vector
linear.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
linear.PlaneVelocity = Vector2.new(knockbackVector.X, knockbackVector.Z) / (1 / knockbackSpeed) -- convert to Vector2 and calculate the knockback velocity
linear.Parent = enemyChar
Could be that LinearVelocity is affecting the target player’s velocity in a way that causes them to rotate. We can add BodyAngularVelocity to the target player’s humanoid, something like this:
-- assuming c is the attacker's character
local vec = c.HumanoidRootPart.CFrame.LookVector
local knockbackDistance = 10 -- adjust as needed
local knockbackSpeed = 120 -- adjust as needed
local knockbackDirection = Vector3.new(vec.X, 0, vec.Z).Unit -- convert to Vector3 and set Z component to 0
local knockbackVector = knockbackDirection * knockbackDistance -- calculate the knockback vector
local linear = Instance.new("LinearVelocity")
linear.Enabled = true
linear.VelocityConstraintMode = Enum.VelocityConstraintMode.Plane
local att = Instance.new("Attachment")
att.CFrame = enemyChar.HumanoidRootPart.CFrame
att.Parent = enemyChar.HumanoidRootPart
linear.Attachment0 = att
linear.MaxForce = 60000
linear.PrimaryTangentAxis = knockbackDirection
linear.SecondaryTangentAxis = Vector3.new(0, 1, 0) -- set to up vector
linear.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
linear.PlaneVelocity = Vector2.new(knockbackVector.X, knockbackVector.Z) / (1 / knockbackSpeed) -- convert to Vector2 and calculate the knockback velocity
linear.Parent = enemyChar
-- add a BodyAngularVelocity to prevent rotation
local bodyAngularVelocity = Instance.new("BodyAngularVelocity")
bodyAngularVelocity.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
bodyAngularVelocity.P = 100000
bodyAngularVelocity.AngularVelocity = Vector3.new(0, 0, 0)
bodyAngularVelocity.Parent = enemyChar.HumanoidRootPart
Ok so it does seem that somehow the linearVelocity is affecting the target player in a way that makes them rotate, we can now actually get moved, although now this is really weird
local Direction = -HumanoidRootPart.CFrame.LookVector --- the knock direction (in this case is the back direction)
local Power = 30 --- power of knockback
local LV = Instance.new("LinearVelocity")
LV.Attachment0 = Attach
LV.ForceLimitMode = Enum.ForceLimitMode.PerAxis
LV.MaxAxesForce = Vector3.new(1,0,1)* 50000 ---- force will only take effect in X and Z ignoring the Y one.
LV.VectorVelocity = Direction * Power
LV.Parent = Character
Adjusting the AxesForce is very helpful when you want to set force, only to specific coordinates.
Sorry for replying 1 year later, hope this help someone.