Vector angles issue

so I know what I’m doing I think… but basically heres my code

--services
local runService = game:GetService("RunService")
--variableItems
local SpotLight = workspace.Part.Attachment.SpotLight
local Character = game.Players.LocalPlayer.CharacterAdded:Wait()
local Torso = Character:WaitForChild("HumanoidRootPart")
--functions
local function Inrange()
	local LightVector = SpotLight.Parent.CFrame.LookVector
	local PlayerVector = Torso.CFrame.UpVector
	local theta = math.acos(LightVector:Dot(PlayerVector)/(LightVector.Magnitude)*(PlayerVector.Magnitude))
	return theta
end

while runService.RenderStepped:Wait() do
	print(Inrange()) -- ~1.57
end

whats the problem you may ask well where we print in the while loop all the way at the bottom its supposed to print some angle but instead it prints some decimal that starts with 1.57. You also may realize that one vector starts from the characters torso when moving the torso in game the decimal does not change even maybe by 0.0000000001 but thats it

so basically I realized that im really dumb and I should have converted theta into degrees but I still have an issue it stays at 90 degrees no matter where I move

From my experience, dot product isn’t always perfect and can sometimes return a number outside of the [-1, 1] range, which causes the arccos function to break. Use math.clamp as a safeguard.

This is Redundant. UpVectors, RightVectors, and LookVectors are already normalized and have a magnitude of one. That’s just extra math and calculations with no purpose.

This is because you’re using the UpVector of the player, which always points up no matter what. I believe you were supposed to use the LookVector of the player, just like what you did with the spotlight.

1 Like

I just fixed some of my code and have already done many of the things you have said but how do I use math.clamp with this?

It should be obvious if you looked it up on the API reference

math.clamp(number, minimum, maximum)
local theta = math.acos(math.clamp(LightVector:Dot(PlayerVector), -1, 1))
1 Like

I did many times I just didn’t get it

--services
local runService = game:GetService("RunService")
--variableItems
local SpotLight = workspace.Part.Attachment.SpotLight
local Character = game.Players.LocalPlayer.CharacterAdded:Wait()
local Torso = Character:WaitForChild("HumanoidRootPart")
--functions
local function Inrange()
	local LightVector = SpotLight.Parent.CFrame.LookVector
	local PlayerVector = Torso.CFrame.LookVector
	local magnitude = (LightVector - PlayerVector).Magnitude
	local theta = math.acos(math.clamp(LightVector:Dot(PlayerVector),-1,1))
	print(math.round(math.deg(theta)))
end

while runService.RenderStepped:Wait() do
	Inrange()
end

this is my current code I get degrees but like i said it stays at mostly 90

is there a way I can create a vector starting from one place and ending at another??? in my case a vector starting from the players torso to the spotlights parent