You would need to apply a rotational offset, in your case 180 degrees. Simply adding that offset to the value should rotate the cone.
local OFFSET = 180
function mainFunctions.isInFOV(rootPosition, enemyPosition, FOV)
local distance = rootPosition - enemyPosition
local radians = math.atan2(distance.Z, distance.X)
local degrees = math.deg(radians) + OFFSET
if degrees > 180 then
degrees = -(360-degrees)
end
local degrees = math.abs(degrees)
if degrees < FOV/2 then
print("true")
return true
else
return false
end
end
local function IsInFov(Anchor, Target, Fov)
if not (typeof(Target) == "Vector3") then
if not Target:IsA("BasePart") then return end
Target = Target.Position
end
if not (typeof(Anchor) == "CFrame") then
if not Anchor:IsA("BasePart") then return end
Anchor = Anchor.CFrame
end
local A = (Anchor.Position - Target).Unit
local B = -Anchor.LookVector
local dp = A:Dot(B)
return(dp > math.clamp(90-Fov, 1, 90)/90)
end
I made four groups of balls located on the x Axis and z Axis. As you can see one group is on the -z, an other on +z, another on the -x and the last one on the +x.
The mid part is the rootPart, we can considerate it like Sasuke.
As you can see, we can see a first difference.
For both screen the FOV is 35 degrees but the Kylerzong’s code seems to give a bigger FOV than 35.
Now, I rotated the rootPart by -90 degrees and here is the result:
(kylerzong’s code)
So as you can see the rotation is kind of messy.
In the kylerzong’s code, the FOV isn’t 35 degrees but it looks like it take into account the rotation of the rootPart. In the xZylter’s code, it looks like to be 35 degrees but it’s doesn’t care about the rotation of the rootPart, only working on Positive x and z groups.
How about those results? Howdo you think I can do to solve this?
Thank you for your advices!
When inputting the fov into the function in my script you would put 35/2
I believe you are doing:
if that’s not the case then you could just play around with the values until you get the fov you want, I tried to relate the fov variable to degrees by just playing around with it
Ok so I just have to divide the FOV by two when I input it in the function?
Mh, Do you know how I make the two lines to show the cone like in the screen shots of all star tower defense.
I think I’ll try to place two parts and to weld them with the character, then they will rotate with the character but I don’t know how to place them, how to have the position of each border of the cone.