Detect if a part is visible in the players view

Since I’m making a FNaF remake game, the screen will turn as you move your cursor, like in the games, until it reaches it’s “Range,” which is just the farthest it can turn (I have it at 20 degrees).

The problem is, depending on the screen, the player might not be able to see the vent on their right side, and sometimes they could see too far ahead. This is hard to control because it’s completely based on their screen size.

Here’s a visual example:

Notice how depending on their screen, they may not be able to see a crucial part of the game?

One idea I had was WorldToScreenPoint, I tried but it didn’t really work how I intended.

Any ideas?

Could you limit the camera angle depending on each player’s screen aspect ratio instead?

1 Like

I never even about thought of that. I’ll try it and update you.

1 Like

Add an invisible wall after each vent, or record the position. When the camera Z (or whatever angle is rotating left/right) passes that point, set it back to that Z point.

Hopefully that makes sense. Old games used to be coded this way for 2D wall detection; rather than the wall preventing the sprite with physics, it was prevented by returned it to its respective position when it passed the wall (while still allowing movement parallel to the wall).

Edit: you can incorporate WorldToScreenPoint here, once the “wall” is onscreen, the camera need not continue.

I tried but I couldn’t figure out a formula to calculate it. Any help with that? I’m confident that it’ll work, though.

Some extra information that I gathered:

Perfect angle for a 16:9 screen is 25 degrees
Perfect angle for a 1:1 square screen is 42 degrees

I’m not sure how that’s going to help but it’s something.

I also think we can just use the screen’s absolute width, not necessarily the aspect ratio.

If 1:1 = 1 = 42°

and 16:9 = 1.7777 = 25°

then if you divide 42° by 1.7777 you get 23.6° which is very close to 25°

So basically 42 (the max angle) divided by the aspect ratio should be pretty close to what you need.

That means a wider 20:9 phone screen would see 42 / 2.2222 = 18.9° as well.

I think another way to do it is just change the players FieldOfView depending on their device aspect ratio. This way the same 20° limit either direction with different FOV could give the same edge limits of the player’s view.

Well, it wasn’t exactly what you said, but it 100% works.

local ratio = screengui.AbsoluteSize.X/screengui.AbsoluteSize.Y
	
range.Value = squareRatioAngle/(ratio-0.09)

squareRatioAngle is 42. Not sure why the 0.09 works, but it does.
I got this from doing 42/65, which gives 1.68.

My thought process was weird to say the least, but it works perfectly!

I’ll count your reply as a solution, since you definitely pointed me in the right direction. I wouldn’t have even thought about the aspect ratio. :smiley:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.