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

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.

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

Answer this question