You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want to make it so the npcs (zombies) being pushed away from me that are really up close also get pushed away.
What is the issue? Include screenshots / videos if possible!
The issue is that when I used my item, the zombies a little bit farther away get pushed back, but the ones really up close arent.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve tried tweaking some of the values + looking it up online, no fix was in sight.
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
The zombie that is really close up to me is getting put into the list, but just not being pushed back.
function ability.KnockBack(player)
local overlapPerms = OverlapParams.new()
local zombiesInArea = {}
overlapPerms.FilterType = Enum.RaycastFilterType.Include
overlapPerms.FilterDescendantsInstances = {Zombies}
local area = workspace:GetPartBoundsInRadius(player.Character.HumanoidRootPart.Position, 20, overlapPerms)
for _, part in ipairs(area) do
local zombie = part.Parent
if zombiesInArea[table.find(zombiesInArea, zombie)] then
continue
end
if AffectedZombies[table.find(AffectedZombies, zombie.Name)] then
table.insert(zombiesInArea, zombie)
end
end
for _, plr in ipairs(Teams.Living:GetPlayers()) do
VisualizeAbility:FireClient(plr, player, "knockback")
end
-- PART THAT MATTERS
for _, zombie in ipairs(zombiesInArea) do
coroutine.wrap(function()
local hrp = zombie.HumanoidRootPart
local humanoid = zombie.Humanoid
local ThrustAttachment = Instance.new("Attachment")
ThrustAttachment.Parent = hrp
local direction = player.Character.HumanoidRootPart.Position - hrp.Position
direction = direction * Vector3.new(20, 30, 20)
direction = direction.Unit
local Thrust = Instance.new("VectorForce")
Thrust.Attachment0 = ThrustAttachment
Thrust.ApplyAtCenterOfMass = true
Thrust.RelativeTo = Enum.ActuatorRelativeTo.World
Thrust.Force = direction * -10000
humanoid.Jump = true
Thrust.Parent = hrp
task.wait(.1)
ThrustAttachment:Destroy()
Thrust:Destroy()
end)()
end
end
Your force is being multiplied by the distance between the character and the NPC.
So, for example, if the NPC is 0 studs away, then the force is zero, while if it’s 100 studs away, the force is 100 * 10000 (i.e. the further the NPC, the larger the force).
To fix this, just change this line:
local direction = player.Character.HumanoidRootPart.Position - hrp.Position
to:
local displacement = player.Character.HumanoidRootPart.Position - hrp.Position
local direction = displacement.Unit -- Creates a 1 length vector in the direction of the displacement
This makes it so the resulting force is independent of the distance by making the direction vector always have a length of 1 (instead of a length of the distance).
EDIT:
Ah, I missed the line where you already made the vector into a unit vector.
did the animation of the zombie have a role? as it was attacking, you shot a pulse at the same time. Does that do anything like anchor the zombie? or just play an animation.
Hey, thank you for the response. This was one of the first methods I’ve tried recently and it generates similar results as my code and does not achieve what I am looking for.
Did you try seeing if they’re being anchored during any attacks? It’s hard to tell from the video but it looks like they don’t get pushed when they’re performing an attack.
Oops, I missed the part where you converted the direction to a unit vector later in the code.
I would try using a LinearVelocity instead of the force. Adding a constant amount of force over a fixed time should just impart a velocity, so they should work the same (though remember to use smaller numbers).
Sometimes humanoids “stick” to the ground. Currently your code uses jumping to solve that, but maybe the NPCs running into your character causes them to get stuck on it, so they don’t end up jumping.
A linear velocity can be set to use as much force as needed to reach the target velocity, so even if they get stuck initially it will pull them out of it. Then you can just delete it a 10th of a second later, at which point the NPC should have already gotten the set velocity (remember to use MaxForce = math.huge or another big number).
The solution to fix this issue was by using BodyVelocity. There’s a MaxForce property embedded into it that would make it easier to apply force to trickier situations. While LinearVelocity works, it would lag moving npcs making it unpleasant to look at.
Thank you for the help.
local hrp = zombie.HumanoidRootPart
local humanoid = zombie.Humanoid
local distance = (hrp.Position - player.Character.HumanoidRootPart.Position)
local forceMagnitude = (30 * Vector3.new(3, 3, 3)) * (1 - (distance.Magnitude / 20)) -- Scale force with distance
local direction = distance.Unit -- Direction away from the central part
local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.Velocity = direction * forceMagnitude
bodyVelocity.P = math.huge
bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bodyVelocity.Parent = hrp
humanoid.Jump = true
bodyVelocity.Parent = hrp
task.wait(.2)
bodyVelocity:Destroy()