I’ve only been using Unity since Monday and I was looking for some advice. I’m working on a submarine game but I can’t find a good way of implementing the sonar. So far I’ve been using raycasting to detect the position of objects but to get good resolution seems to require thousands of rays and this slows the game down massively.
I was also wondering what the best way to display the output would be. At the moment I’m using GUI.DrawTexture and drawing the result of each ray cast as a pixel (with the colour determined by how far away the object is) but I’m guessing this is not the best way of going about this.
Any advice on alternative methods or how to proceed would be greatly appreciated.
Since you already know the position of your objects in the scene why not just update their position relative to the center of the sonar (current position of player?) and just map that to the SONAR graphic?
That way you wouldnt have to cast thousands of rays, you could just access your game objects position and do quick vector math to determine where on the sonar they should appear per frame
To image objects, you don’t need to raycast thousands of times (unless you have thousands of objects). You just raycast to each object that is in range to determine line of sight. Collect all of the objects in range and that have line-of-sight.
Then you can create a sonar display not unlike the radar script floating around out there (might be on wiki). Basically, I think it is a sonar texture with ‘blip’ textures drawn on top (I’ve never looked at it). Use the distance of the real objects from the sub, but scale them down to the sonar’s range (ie. if the sonar displays 1/10 scale, then divide objectPosition-subPosition by 10). That’ll give you the distance from the center of the sonar display, and place the blip there.
Blip intensity can just be handled by changing the texture brightness (don’t know how, but know it can be done) based on some mathematical relationship to distance.
I know I could be more detailed, but that would require thought, and I haven’t the time for that!! Seriously though, hope this gives you some direction.
I’ve had a look at the radar script and it is much more efficient, but as I understand it the blip appears at the central position of the object. However I’d like the sonar to be able to map objects shape and orientation. Does anyone know of a way to do this?
best i could tell would be to make small textures for each unique shape that you can have in your objects.
Then find a way to map the texture to the rotation that the game object is in. If the game object is rotated 90 degrees rotate the texture to match. this would of course get tricky with multi-axis rotations.
You may also be able to use a camera at each object and render the object to texture to get the silhouette and somehow generate a texture based on that for the GUI.
The mechanics of getting this stuff to work is beyond my ability atm with Unity so I am just throwing ideas out there.
I think your second idea may well be a good one. Would it be possible to use just one extra camera but format the image from this camera in such a way that it looks like sonar (ie. render the colours of the objects differently than the rendering in the main screen)?