Dot Product returning numbers greater than 1

Hello everyone!

Im trying to make use of the :Dot() function, but it doesn’t seem to be working properly. From my understanding, dot is intended to give you a value between -1 and 1 to show how closely two vector are facing in the same direction. Unfortunately, my attempts at using it have resulted in numbers upwards of 40,000+ depending on the distance between the two points.

Here is my code:

local root = script.Parent.Parent:WaitForChild("HumanoidRootPart")
local enemyModel = workspace:WaitForChild("Entities"):WaitForChild("Dummy")

game:GetService("UserInputService").InputBegan:Connect(function(obj,gameProcess)
	if obj.KeyCode == Enum.KeyCode.R then
		local dot = root.Position:Dot(enemyModel.HumanoidRootPart.Position)
		print(dot)
	end
end)

My goal with using Dot Products is to make a system that can tell if a player is in front of another player or not.

Vector3:Dot(Vector3) expects 2 unit vectors representing the look direction.
In programming terms, this would be your LookVector.

What you’re doing wrong is you’re getting the dot product of your player’s position to the enemy’s position, when you should be getting the dot product of your player’s look direction to the enemy’s look direction.

Simple as changing root.Position to root.CFrame.LookVector and same with the enemyModel position.

Hope this helps! :slight_smile:

2 Likes