LinearVelocity "teleport/skipping" on knockback

I have been trying to make a knockback system in my combat game. The thing is, whenever this rush move gets used, everything is okay, apart from how the knockback/linear velocity acts. The knockback should happen from where the player attacking faces, but the enemy usually sinks into the floor a bit, then sort of jolts and instantly teleports a bit forward. I am confused on how to solve this.

Here is what is happening:

External Media

Here is the code below:

local collectionService = game:GetService("CollectionService")

coolerMove1Remote.OnServerEvent:Connect(function(client, rushData)
	local c = rushData.Character
	local enemyPart = rushData.Target.HumanoidRootPart
	local enemyHuman = rushData.Target.Humanoid
	local hrp = c.HumanoidRootPart
	enemyHuman:TakeDamage(20)
	local enemyLookVector = hrp.CFrame.LookVector * -1
	if rushData.Action == "Rushed" then
		c.Humanoid.AutoRotate = false
		local lv = Instance.new("LinearVelocity")
		local a0 = Instance.new("Attachment")
		lv.Attachment0 = a0
		lv.Parent = a0
		a0.Parent = enemyPart
		lv.MaxForce = math.huge
		lv.VectorVelocity = enemyLookVector * -75
		if rushData.Target then
			lv.VectorVelocity = enemyLookVector * -75
			coolerMove1Remote:FireClient(client, "TargetFound")
			lv:Destroy()
			a0:Destroy()
		end
		task.wait(.2)
		enemyPart.CFrame = hrp.CFrame:ToWorldSpace(CFrame.new(0,0,-2))
		c.Humanoid.AutoRotate = true
		task.wait(.6)
		local attachment = Instance.new("Attachment")
		attachment.Parent = enemyPart
		local lv = Instance.new("LinearVelocity")
		lv.Parent = attachment     
		lv.MaxForce = math.huge
		lv.VectorVelocity = enemyLookVector * -60
		lv.Attachment0 = attachment
		game.Debris:AddItem(attachment, .5)
		game.Debris:AddItem(lv,.5)
	end	
end)

I want to mostly want to understand whether I do not have a basic understanding of linear velocity, because I only just started trying to use linear velocity, so I need help seeing what is causing this problem.

1 Like

Try getting rid of the Y Vector3 on the LinearVelocity because the look vector could have negative Y in it making the NPC go into the ground.

-- Replace both lines with this
lv.MaxForce = Vector3.new(math.huge, 0, math.huge)

That wouldn’t work though would it? Because its a Vector3 and not a number?

Alright, I found the solution. Getting rid of the Y Vector3 on LinearVelocity idea helped me.

I had to make the Linear Velocity ForceLimitMode to be PerAxis.
I had to also add a VelocityConstraintMode to be Vector.
Then this makes me have to use MaxAxesForce and not MaxForce since MaxForce is a number while MaxAxesForce allows for each of the Axis.

Code below that I needed to replace/add:

lv.ForceLimitMode = 1
lv.VelocityConstraintMode = 2
lv.MaxAxesForce = Vector3.new(math.huge, 0, math.huge)

Thanks for the help/idea! I was very unknowledgeable to Linear Velocities but that idea helped me find out more about Linear Velocity.

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