Multiplying LookVector with Numeral keeps giving opposite directions

Hello, I’m making a combat system for a little project of mine, yet I’ve found a problem that is related to the knockback attribute. Whenever it grabs the lookvector to calculate the distance the player will be knocked back, it either pushes the attacked player towards the attacker, or pushes them away (I’m using a VectorForce instance to do so), it seems to switch between the two at random.

I’ve tried switching from using the lookvector to grabbing the direction by using .Unit, but that has given the same results.

Any help would be greatly appreciated.

ModuleScript

function Apply:KnockBack(EnemyCharacter:Model, PlayerHumanoidRootPart:Part, UpVector:Vector3, Force:number)
	if runServ:IsClient() then
		error("Attributes must be changed on the server")
	end
	Force = Force or 10
	
	local conf = findConf(EnemyCharacter)
	local knockBack:BoolValue = conf:FindFirstChild("KnockBack")
	
	--Calculates the force 
	Force = (Force * 250)
	local Direction = (PlayerHumanoidRootPart.CFrame.LookVector * Force) + UpVector
	knockBack.Direction.Value = Direction
	
	--Applies the force to the player
	knockBack.Value = true
end
1 Like

There are a couple weak points in this function:

  1. If the force is negative, it will invert the direction of the look-vector
  2. If the UpVector is incorrectly given, it could affect the knockback direction in any number of ways

Set up debug points in your function to investigate these inputs and see which might be causing the problem. On another note, I’m not exactly sure why you’re multiplying the input force by 250, but you can simplify your code to include that multiplication in the defaulting code:

Force = (Force or 10) * 250

I would also avoid using a variable name like “conf” as it’s a needless simplification that only harms readability. If it is meant to mean “configuration”, type it out as such. Concise does not mean fragemented

3 Likes

Turns out I never set the vector force RelativeTo Enum as the World, which was causing it to push the player relative to their model instead of pushing them relative to the world. Either way, thanks for the advice on code etiquette.

2 Likes

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