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