What is the best way to make a knock back system?

I’m attempting to make a combat system with knockback as MC has. My question is, what is the best way to set the velocity of a player based on the direction you hit them?

I’m looking for a really clean way to do this. I want to simulate the effect of getting hit by a block essentially.

8 Likes

I surfed the stack exchange for information on this:

It looks like knockback on weapons in that game is a flat value that is oriented away from the attacker. It’s your choice as to whether you want to add this knockback value to the already-existing velocity, or just make it a flat value. The former would definitely be more dynamic.

If you want to make the combat system identical to MC, you probably also have to switch over to their form of hit detection, identical to just raycasting from the player’s camera (or head) a couple studs.
Pseudocode:

-- on activation:

  -- create ray from camera and elongate it (a little)

  -- get part from casting ray

  -- if part exists, look for a humanoid or smth similar

  -- if humanoid exists, fire remote event to server

  -- server checks distance from victim, and if satisfactory,

    -- server deals damage and applies knockback to its root part

The knockback formula would probably look like this:

local ConvictHRP = -- root part of guy who gave damage

local VictimHRP = -- root part of guy who received damage

local Angle = 
-- set horizontal knockback portion
((VictimHRP.Position - ConvictHRP.Position) * 
    Vector3.new(1, 0, 1)).Unit * 10 +
-- set vertical knockback portion
    Vector3.new(0, 10, 0)

VictimHRP.Velocity = VictimHRP.Velocity + Angle

The 10’s in the knockback formula are going to be the numbers you will want to play with.

22 Likes

Thanks! I edited it a little bit and found the best way was to use body velocity rather than setting the velocity of the raw player

while wait(2) do	 
	local velo = Instance.new("BodyVelocity",workspace.Stratiz.PrimaryPart)
	velo.MaxForce = Vector3.new(10000000,10000000,1000000)
	velo.P = 1000000

	local ConvictHRP = workspace.E
	
	local VictimHRP = workspace.Stratiz.PrimaryPart
	
	local Angle = ((VictimHRP.Position - ConvictHRP.Position) * Vector3.new(10,0,10)).Unit * 50 + Vector3.new(0,25,0)
	
	velo.Velocity = Angle
	wait(0.1)
	velo:Destroy()
end
20 Likes

This really helped me. Thank you!

1 Like