Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

3 comments

0

you could fire a remote event if the mouse target of player is a body part of the monster

0

you can use the PathfindingService to make the monster move to the player

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.

1 answer

✓ Accepted answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.