I've looked at WorldToScreenPoint and WorldToViewportPoint but I don't really think it is that effective considering my game still needs 3rd person. I don't really see raycasting as an option as to effectively consider the player's full viewing angle, we'd need a lot of rays. I don't need a method to be 100% accurate in actually being seen, but enough so that I don't have to look directly at the object.
As you said, you want WorldToScreenPoint
or whatever, but you also want to use GetPartsObscuringTarget
.
What you actually want is "Character facing towards part", not "Player looking at part"
You can do this with vectors:
player = game:GetService("Players").LocalPlayer character = player.Character or player.CharacterAdded:wait() torso = character:WaitForChild("Humanoid").RootPart target = workspace:WaitForChild("LookyLou") vectors = {} function dotProduct(a, b) return a.x * b.x + a.y * b.y + a.z * b.z end function getAngle(a, b) return math.acos(dotProduct(a, b) / (a.magnitude * b.magnitude)) end function drawVector(position, angle, magnitude, name) if vectors[name] then vectors[name]:Destroy() end local p = Instance.new("Part") p.Anchored = true p.Size = Vector3.new(0.1, 0.1, magnitude) p.CFrame = CFrame.new(position + angle * magnitude / 2, position + angle) p.CanCollide = false p.Parent = workspace vectors[name] = p end game:GetService("RunService").RenderStepped:Connect(function() local torsoAngle = torso.CFrame.lookVector local targetVector = torso.Position - target.Position local flatTorsoAngle = Vector3.new(torsoAngle.x, 0, torsoAngle.z) local flatTargetVector = Vector3.new(targetVector.x, 0, targetVector.z) drawVector(torso.Position - Vector3.new(0, 2.5, 0), flatTorsoAngle, flatTargetVector.magnitude, "torso") drawVector(target.Position, flatTargetVector.unit, flatTargetVector.magnitude, "target") local angle = math.deg(getAngle(-flatTorsoAngle, flatTargetVector)) if angle > 40 then target.BrickColor = BrickColor.new("Bright red") else target.BrickColor = BrickColor.new("Bright green") end end)
Put a brick in the workspace called "LookyLou" -- it will go red when you're within 40 degrees of facing it.