local lookVector = part.CFrame.LookVector
local up = Vector3.new(0, 1, 0);
local down = Vector3.new(0, -1, 0);
local left = Vector3.new(-1, 0, 0);
local right = Vector3.new(1, 0, 0);
local forward = Vector3.new(0, 0, -1);
local back = Vector3.new(0, 0, 1);
local upDot = lookVector:Dot(up)
local downDot = lookVector:Dot(down)
local leftDot = lookVector:Dot(left)
local rightDot = lookVector:Dot(right)
local forwardDot = lookVector:Dot(forward)
local backDot = lookVector:Dot(back)
local facingDot = math.max(upDot, downDot, leftDot, rightDot, forwardDot, backDot)
if facingDot == upDot then
print("Facing up")
elseif facingDot == downDot then
print("Facing down")
-- elseif facingDot == rightDot then -- etc.
end
It is perfectly necessary; it checks every direction and compares that Dot product to the look vector of the given part’s CFrame to see which direction in world space that it is closest.
And if you want to check if the part is facing in a certain direction by how many degrees, use this:
local desiredDirection = Vector3.new(0, 1, 0)
local currentPartDirection = part.CFrame.LookVector
local angleInDegrees = math.deg(math.acos(math.clamp(desiredDirection:Dot(currentPartDirection), -1, 1)))
if angleInDegrees < 30 then
print("Part is facing towards target within a 30 degree radius!")
end
This is a more unorthodox way of doing this, but you could try it like this, you could set parts that are set as west east etc. And you could send relevant code when it senses which part it is. The code up above is a much better way to do it, but this is possibly an easier route. I accidentally spelled relevant wrong