Body Velocity Problem

this is what I use for knockback but the problem is that when I am behind the enemy they will sometimes instead of being knocked away from the player they are being knocked in the direction of the player:

local function NormalVelocity(hrp, number)	
	if hrp.Parent:FindFirstChild("SmallMoveVel") then
		hrp.Parent:FindFirstChild("SmallMoveVel"):Destroy()
	end

	local vel = Instance.new("BodyVelocity")
	vel.MaxForce = Vector3.new(1,1,1) * 1000000;
	vel.Parent = hrp
	vel.Velocity = Vector3.new(1,1,1) * hrp.CFrame.LookVector * number
	vel.Name  =  "SmallMoveVel"
	Debris:AddItem(vel,.3)
end
NormalVelocity(enemyHRP, -5)

Video:

1 Like

I may be looking at this wrong but are you not just setting it to send the enemy backwards based upon their lookvector?

1 Like

oh yea lol, thanks this was it actually!

2 Likes

Yeah I see the problem here. As one of the other replies said, you’re setting the velocity to the enemies look vector, what you need to do is really find the direction between you and the enemy, which thankfully is very simple.

As it turns out, Roblox’s engine comes with built in math functions for Vectors, the one we will be using is the .Unit function, what this does is essentially it normalizes the given vector to a unit vector (correct me if I’m wrong). Here is how it would be used:

local MyHRP = -- HRP LOCATION
local EnemyHRP -- ENEMIES HRP LOCATION

local Direction = (MyHRP.Position - EnemyHRP.Position).Unit

That’s really it. All you would do then is just multiply Direction by an arbitrary number for the strength of the velocity. What you need to make sure of is that the first Position you input is what you’re travelling away from and the second is what you’re travelling to, as you can see I set your own position as the first and minus’d it by the enemies position, now if you do make the mistake and flip them, all you need to really do is to multiply by a minus value instead, it doesn’t really make a difference. Hope this helps!

2 Likes

Also still helped thanks for that!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.