I’m trying to get a target via raycasting from a varying position on the screen, but don’t want to target things off screen. I was thinking of getting the magnitude of a vector from the position on screen to the screen edge and setting that to the max distance of the ray to that. I don’t however know how to do this. I know how to get the edge of the screen, but not a specific points along that edge. Halp!
PS the camera is stationary and it’s a top-down game.
I’m not completely sure if this is what you’re saying, but if it is, you COULD see if the object you’re targeting is off-screen, and if not, ignore it, rather than making the raycast not hit beyond the screen.
Of course, if you wanted to do that, you could make a collider around the edge of the screen, and only let the raycast hit it. Both would work really.
In addition to what @RiokuTheSlayer said, you can test the renderer like so :
if( renderer.isVisible == true )
But BEWARE, in Edit mode, if your Scene window can “see” the objects, it will be triggered as isVisible = true;
And as RiokuTheSlayer said, if it’s not what you were needing, sorry, or maybe explain things a bit more with some code so we can try to help!
All of your suggestions are great, and I may just do the WorldToScreenPoint. I’m already using it for other things, but it just seemed cheaper to only cast to the screen edge. But I guess since I’d have to calculate that magnitude every frame anyway… hmm… thinking outloud. Ok ya, the WorldToScreenPoint will work best. Thanks guys!