OK, here’s what I got so far. This doesn’t work, so I’m thinking that the colliders that are hit need to be put in an array or string and the mesh renderer needs to be procured a different way.
I wonder too if it wouldn’t be better to use an OverlapSphere instead of raycasting? Isn’t there a limit to the number of raycastings you can use, or maybe the processing bogs down the processor?
Also, with this approach the computer doesn’t have to render the “out of sight” objects, but they do have to keep track of the geometry, so maybe this would be OK for a racetrack but maybe not the answer for a detailed dungeon for instance. Instead loading and unloading scenes might be better in that case.
// Raycast up to 100 meters forward and get colliders that are hit
function Update () {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, Vector3.forward, hit, 100.0)) {
collidersHit = hit.collider;
//
colliderHit.renderer.enabled = true;
}
colliderHit.renderer.enabled = false;
}
========== Edit ============
This version uses arrays and for statements to iterate through but still is not working:
// Raycast up to 100 meters forward and get colliders that are hit
function Update () {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, Vector3.forward, hit, 100.0)) {
//make new variable array
var collidersHit = new Array ();
collidersHit = hit.collider;
// make each object in the array visible
for (var value : String in collidersHit)
{
colliderHit.renderer.enabled = true;
}
}
// make each object in the array invisible
for (var value : String in collidersHit)
{
colliderHit.renderer.enabled = false;
}
}
…after thinking about this awhile, instead of how I’m calling the make invisible, think I may need another array with all objects in the scene and subtract the ones that are hit by the raycast, and make the remaining objects invisible…
There’s probably a few more ways to go about this:
-OverlapSphere
all objects have renderer as false, when hit with OverlapSphere set to true or maybe this could be done with a simple large collider parented to the camera
-Could also do this by getting distance from object to camera and make object not render if beyond certain distance.
I’m not sure what would be less taxing on the game runtime. Anybody?