Using touch to shoot in 3D

I’ve just written a blog post about touching objects in a 3D scene to shoot at them. The challenge is taking a 2D touch and figuring out how close you came to hitting the object in 3D. There have been various solutions posted before, including using math (fastest, if your objects are simple, regular shapes), and shaders to draw a second copy of the scene with tagged objects.

But I like this one better. It figures out the direction of your shot, in 3D, moves along it to the same distance as the target, and then measures the distance between them (effectively, just as your shot passes the target). It will work for any type of target object and doesn’t require too many calculations.

The blog post tries to explain how it works, but if you just want the code, there is a handy function and sample code to try.

I thought I’d also add that for those who don’t know this, there is a really easy way to add a zoom feature to a 3D app. When using the perspective command, the first parameter is field of view, which defaults to 45 degrees. If you make it smaller, eg 25, it effectively zooms in. You can make it very small indeed if you want.

sometime i’ll study it carefully

Thanks this really helps