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.
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.