First of all thank you to everyone in this community. Although I have not yet posted, I have gathered a lot of information from here. I unfortunately have hit my first big wall in the development of my game, and hope the community can help me overcome it.
I am making a 3D space shooter game in the vein of FreeSpace. I have a targeting system implemented, and when the target is off screen, I would like there to be an arrow on the side, top, or bottom of the screen pointing in the direction the player must turn in order to face his target.
The mathematics of this are quite mind boggling to me unfortunately. I have only a limited understanding of WorldToViewportPoint to begin with, and there is not much documentation or discussion on it. I do know how to find if an object is on screen, but how the actual values of x, y and z are calculated remains a mystery to me. X and Y can have some pretty bizarre values, and I know Z has something to do with the distance to the target, but I can’t put it all together.
If someone can help me get started, I would very much appreciate it.
For worldToViewport, the resultant x and y values are the position on the screen that the ‘worldPoint’ would be rendered at from the chosen camera. If they are outside of the viewed ‘area’, the values will be less than 0 or greater than width or height, and as the angle from the camera increases, the values approach infinity and negative infinity respectively.
When the ‘worldPoint’ goes behind the camera, the x and y values begin to come back down again, and that is where the z coordinate comes in- it gives the depth along the forward vector of the camera of the object- if it is 10 units away, it wil be 10. When an object is behind the camera, the z-coordinate will be negative- this allows you to know if the viewport position of an object should still be offscreen even if the x and y coordinates seem to be within the bounds of the viewport.
I have something just like this on the Asset Store: http://u3d.as/content/gdcore/radar-arrows. It not only will point off-screen, around the perimeter of the screen, it also gives the option of hovering above on-screen objects.
I stumbled on this old post after running into the same problem myself.
I got this to work by simply setting the ScreenPoint to (infinity,infinity, infinity), thus throwing it offscreen. Perhaps not the prettiest solution, but it is simple and it is working!