I am making a first person 1 player horror game, where the monster moves when you're not looking at it. (like Dark Deception) I have the movements, I just need to figure out how to detect if I am looking away. If anyone can advise me on what to do.
COMMUNITY QUESTION
How do I detect if I am looking at a part?
1 answer
You'll want to get the angle between the vector that represents the player's facing, and the vector between the player and the enemy. You can do this by normalising the two vectors, and dotting them. This will return a value in the range -1 to 1, where 1 represents perfect alignment, and -1 representing completely opposite directions.
https://chortle.ccsu.edu/VectorLessons/vch09/vch09_6.html
You can use this value in a if statement to get the monster to move conditionally (dot < 1?)
local player = xxx.Character
local enemy =
local lookDir = player.Head.CFrame.lookVector
local toEnemy = player.PrimaryPart.CFrame.p - enemy.PrimaryPart.CFrame.p
local cosAng = lookDir.Unit:Dot(toEnemy.Unit)
if (cosAng < 1) then
--move enemy
end
Untested, but that's the theory.
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
Thank you so much, I'll try it.