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

How to see if a player is looking an an object?

Asked by 5 years ago

not asking for code, just on where to start

I want to create a monster type of thing that when you look at him, he'll start moving your way, but I don't know how to do the first part, and I'm looking forward to learn how to start.

Thanks

0
you could fire a remote event if the mouse target of player is a body part of the monster theking48989987 2147 — 5y
0
you can use the PathfindingService to make the monster move to the player User#23365 30 — 5y
0
You could raycast infront of the character to see if it's hitting the monster, or calculate with the help of rotation and angles if he's in x degrees field of view. gullet 471 — 5y

1 answer

Log in to vote
0
Answered by
RayCurse 1518 Moderation Voter
5 years ago
Edited 5 years ago

You could use the function WorldToScreenPoint() which is a method of Camera. It takes in one parameter representing the 3D position that you want to test. It then returns two values: a Vector3 whose x and y properties represent the position on screen (the z property of the Vector3 represents the depth of the 3D point relative to the screen) and a bool representing whether or not the 3D position is on the screen. For your purposes, the only value you need is the bool indicating whether or not the point is within the bounds of the screen. An example is demonstrated below:

function positionIsOnScreen (pos)
    local camera = workspace.CurrentCamera
    local _ , onScreen = camera:WorldToScreenPoint(pos)
    return onScreen
end
Ad

Answer this question