Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I detect if I am looking at a part?

Asked by 4 years ago

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.

1 answer

Log in to vote
0
Answered by 4 years ago

https://imgur.com/a/soixPQa

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?)

01local player = xxx.Character
02local enemy =
03 
04local lookDir = player.Head.CFrame.lookVector
05local toEnemy = player.PrimaryPart.CFrame.p - enemy.PrimaryPart.CFrame.p
06 
07local cosAng = lookDir.Unit:Dot(toEnemy.Unit)
08 
09if (cosAng < 1) then
10--move enemy
11end

Untested, but that's the theory.

0
Thank you so much, I'll try it. AXELProductionsRL 0 — 4y
Ad

Answer this question