I’m working on a combat system and one thing I see in a lot of games is small pushes to the player for different things mainly when they do an attack they get pushed forward and when they hit someone blocking they get pushed back and I don’t understand how this is done. Looking to get some insight to try and hopefully implement it into my code.
-- @param attacker player object
-- @param defender model
local function hitRegister(attacker, defender)
local defender = Server:GetPlayerByInstance(defender) or Server:GetPlayerByInstance(game.Players:GetPlayerFromCharacter(defender))
if not defender then return end
local defenderCharacter = defender.character
local defenderRoot = defenderCharacter.HumanoidRootPart
local attackerCharacter = attacker.character
local attackerRoot = attackerCharacter.HumanoidRootPart
local attack = attacker.currentMove
if defender:isBlocking() then
defender:setHitStun(attack.blockStunValue)
--defender:applyAttackDamage(attack.chipDamage)
else
defender:setHitStun(attack.hitStunValue)
defender:applyAttackDamage(attack.damage)
Effects:FireAllClients("Effects","flash",defender.character,Color3.fromRGB(255, 0, 0))
end
if defender:isInHitStunState() then
attacker:IncreaseCombo()
else
attacker:ResetCombo()
end
end
yeah, I know about that, but I don’t know how to apply it. I’ve looked at a few games and the way they seem to do it is when an action is made the player gets pushed forward (the distance is probably tied to the attack they’re doing) but if they hit someone blocking they get pushed back instead of forward and this is what confuses me because if the player gets pushed forward whenever they make an attack, but then gets pushed back when they hit someone blocking wouldn’t the push forward from doing an input resolve first?
For the pushing forward, you could use the :ApplyImpulse() function on the HumanoidRootPart. You could also use a LinearForce or a LinearVelocity as is mentioned by @AmoraFolf, and then quickly remove it by either the Debris service or destroying it. Just to push forward, you can set the Vector3 used for the push in the direction of where the player is looking. This can be applied by using the HumanoidRootPart.LookVector as the direction and then multiplying by a factor relative to the magnitude of the force you want to apply. To push backward, you just have to use the inverse of the LookVector (-HumanoidRootPart.LookVector). Please tell me if you require further help.
Alright so here’s my version and the VectorForce seems to work nicely, but I still can’t figure out how to prevent the push forward if attacking a blocking person.
Here’s an example of what I’d like to accomplish.
edit: game is Souls Combat Remastered
To prevent the push forward, you can check if the target in the hitbox is blocking before applying the push forward force, and not applying it if they are.
You Could Simply Use Body Velocity’s. I Know They’re Deprecated But They Work And Function Really Well! You can Also Use Linear Velocity’s But I Dont Prefer Them.