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

How can I tell if an object is within the Camera's FieldOfView?

Asked by 10 years ago

I've created a flashbang that gets every player and gives them a white screen according to their distance and whether or not the path between the center of the explosion and the torso of the player is clear. But what I'm trying to do is detect whether the explosion was visible on the Camera (I have a function that creates a shockwave), and then detect how far the explosion center is offset from where the camera is looking. I don't know if you can understand this, but please help.

0
I understand what you're getting at. I don't really have a solution, or code to provide, but you could try creating a Ray originating from the front of all Player's face, and have that detect if it hits a thrown Flashbang. SlickPwner 534 — 10y

2 answers

Log in to vote
0
Answered by
ipiano 120
10 years ago

I've been working on the opposite of this, but a lot of what I've done would be applicable. I'm not sure exactly what the numbers would be for you, but what I'm doing is making a part move so that it always stays just onscreen, right at the edge. Basically, what I would suggest, is using the opposite of my code, which moves a part so that it is onscreen in the same location as a GUI, and use it to make a gui on screen at the part(Like a billboardGUI, but with its location relative to the full screen, not to the part), and then check what the GUI's location is. If it's off screen, the explosion is out of view. Does that make sense?

Unfortunately, not all of my data will work for you, because, since I'm going from 2D to 3D, I just set the Z axis at a delta of 3, but you would need to use more data to figure out how that axis would figure in. (In my scenario, the z axis is the lookvector of the camera)

Here's my relevant code:

    local totalX = script.Parent.FullScreen.AbsoluteSize.x
    local totalY = script.Parent.FullScreen.AbsoluteSize.y
    local targetX = targetUI.AbsolutePosition.x - .5*totalX
    local targetY = targetUI.Position.Y.Scale
    local posdeltax = 4.178/(totalY/targetX)
    local posdeltay = -(targetY-.5)/.25
    local posdeltaz = 3

    local position = focus.CFrame + (focus.CFrame.lookVector.unit*posdeltaz) + (up.CFrame.lookVector.unit*posdeltay)+(right.CFrame.lookVector.unit*posdeltax)

And I've got an excel sheet of raw data that I used to derive the 4.178 and .25. Those values are only accurate when z == 3. If you decide to use this, let me know and I'll get the excel sheet to you.

Ad
Log in to vote
-1
Answered by 10 years ago

What you could do is put an invisible block that is connected to the face of the player and if the flashbang goes inside or touches the block you make an onTouch function.

Answer this question