WorldToViewportPoint not working as expected

Hey, I am using the WorldToViewportPoint() function and I am getting some behaviour that is not wanted.

Basically, I use this on the client-side (yes, I know, clientside) to see if the area someone is clicking is on the screen. However, it work’s fine - UNLESS - you click on the skybox. I get a false, that says that the point is not on the screen. So, it returns: false.

Although I “technically” is on my screen. The reason for this, I think, is that the distance is so far away that it registers it as not on the screen. And I get that, but basically, I want it to return true. I just want to see if the area (no matter how far away) is technically on the screen of the player.

Hope you get what I mean. Thanks!!

What you could do is get the point using the parametric ray equation p=o+(d*t)
where o is the origin and d is the direction and t is how far to travel along (say 5000) studs. Then form a vector from that point to the origin and then check if its dot product to the players look vector is above a certain degree (remember that dot product just returns the ratio between 2 vectors relative to the unit circle)

I’m a little confused. If your script fires when the player clicks, why do you need to check if it’s on the screen? If they clicked, then you can already assume their mouse is on the screen in game.

MouseClick should only fire when the player is in game?..

Sorry, I probably don’t understand.

Hey, no problem. I think I phrased myself a little bad, anyways.

Basically, the reason I want to check if the point they clicked is on their screen is to prevent exploiters from giving a value that they cannot physically hit with the mouse.

1 Like

I understand now, very cool idea!

I wouldn’t forget that an exploiter could just flip the camera around instantly and click. So basically any vector in game could be returned anyways.

Also WorldToViewportPoint doesn’t take into account if there is something blocking between the player and the point. If this is an issue I would cross reference with a quick raycast.

I appreciate your answer.

Actually, I thought this might be an issue, too. But, it turns out that it does work, as it doesn’t send a ray BUT it just chekcs if the place is on the player screen, and if you are going to click somewhere you obviously have to see it.

I thought about that too, it’s a good point. But an exploiter would still have to make a system for it.

However, I don’t know how I could prevent it from returning false when clicking the skybox.

1 Like

Yeah I see how this is an issue for you.

As you previously stated, WorldToViewportPoint is returning false because there’s no specific point to find when you’re pointing at the air (which is infinitely empty).

Honestly this all seems a bit backward and unnecessary to do. There’s got to be a better way for finding exploiters in whatever situation you need this system for than trying to validate their mouse position constantly.

If I were to do this however, I would manipulate Mouse.X and Mouse.Y which give you actual x and y values of where their mouse is on the screen. You can compare this to the player’s absolute screen size and determine if the number is outside of what is reasonably possible.

You could add this back into WorldToViewportPoint and comepare what it returns with Mouse.X and Mouse.Y. If WorldToViewportPoint returns false then find what the mouse position is using Mouse.X and Mouse.Y.

Here’s a link to the mouse object’s full documentation.