Bodyvelocity Knockback Flinging

For my game I’ve included knockback from sword clashes, however when clashing with enemies. If we’re close to a wall and I hit an npc into the wall, or they hit me. The person flung into the wall gets flung. (Excessively)

I’ve tried a solution which is casting a ray behind the person being knocked back to see if there’s room to be knocked back, but it isn’t the most reliable.

From playing elemental battlegrounds, I can frontflip headfirst into a wall and not get flung, I have no idea how to replicate this for my game.

Any help on ideas to prevent flinging will be most appreciated.

if HasRoomBehind(Character,10) then
local Speed = Instance.new(“BodyVelocity”)
Speed.Parent = Character.HumanoidRootPart
Speed.P = 0
Speed.MaxForce = Vector3.new(math.huge,0,math.huge)
Speed.Velocity = Self.HumanoidRootPart.CFrame.lookVector*15
Debris:AddItem(Speed,0.1)
end

1 Like

This is probably why
You’re assigning the MaxForce to literally a number that calculators the size of Pluto can’t even comprehend, maybe try lowering the Force down to like 25000-50000 if that fixes anything?

I see your point, whilst that seems like a fair solution. Most of my NPCs are varied in mass, is there any formula to figure out the minimum amount of forced needed to be exerted to move a model?

I believe so, using the GetMass() function & a bit of calculation here (Had to look this up a bit):

function GetMassOfModel(model)
    local mass = 0
	for i, v in pairs(model:GetChildren()) do
	    if v:IsA('BasePart') or v:IsA('Union') then
            mass = mass + v:GetMass()
        end
    end
    return mass
end

It’d be something relevant like that, where the model is your NPC Model and it’ll return the Total Mass of the NPC

3 Likes

Thank you for bringing me to light about the :GetMass() function

1 Like

Np! Everyday you learn something new, after all! Take that into consideration & use the knowledge gained from it!

For anyone else reading this thread who is interested, make sure to multiply the mass by a certain amount otherwise it won’t generate enough force. I’ve multiplied it by 5000 to move it similar to if you used math.huge

I’m curious, how does this function work to fix flinging and how do I implement it?

I believe the function creates a variable which will set its current Mass at 0 for now, then provided by the parameter (Model), we’ll get all its children then check if every Object we’re looking for is a valid BasePart or Union object

If it is, we can go ahead and add to our Mass variable then return it to retrieve it later & set its MaxForce property

sorry to bump but wht maths do i do once i have the mass?

-- x >= 10
MaxForce = Vector3.new(Mass*x,Mass*x,Mass*x

Multiply the mass since alone, it would not be powerful enough.

If you mean what is they’re doing in the GetMassOfModel function, you are storing the mass of each part into one variable.

wait so what exactly is the variable x over here?

A multiplier of your choice, I just recommend it being greater than ten.

1 Like

Sorry this is a very late response but I got redlines when I tried to put the Mass variable inside the maxforce thingy. how do I fix it exactly?