Hello!I was wondering how can i add the objects thay are in the camera view to an array?What i intend to do is to have a script so i disable/enable the objects i am not looking at.Is there any easy way or i just gotta use a Physics.SphereCast and then check distance,or using normalized vectors?..or is there any another way?
Which method is the best?
Oh,and i ment to write in the title “What does the camera see?”,sorry
Please help me
Doesn’t unity cull what isn’t in camera automatically?
Exactly what I thought. Unity is using Umbra’s technology culling, you can specify on camera what to cull however.
Wellll,i dont need it for performance.I need it because i have a procedurally generated quadsphere that is chucked into hundreds of pieces.So i want to know what chuncks the camera actually sees,so i can activate/deactivate them
If your objects are seperate from one another you might be able to keep them in an array and use isVisible to check what the camera thinks it can see (also see onBecomeVisible)
Or you can check if a given point in space is currently on camera with WorldToScreenPoint, just check that the coordinate is between zero and the screen size
Wow!That can be usefull!
But it requires a renderer so i can’t use it
So what i have generated works like this : I have my planet,which is a simple empty gameObject.Then it generates many many empty gameObjects which are added as childs.Each of those gameobjects have a script attached to them,that generates the mesh,but it is disabled.This is my planet.When my camera comes in a certain range to an empty game object,the script it has enables.If it gets out of that range,the game object’s renderer is disabled / the whole object is disabled.This is so my player does not have to generate the mesh each time it gets close to the surface,but just enable it.
So i am looking for the fastest way to add the objects that the camera “would” see if they had a mesh.I don’t want it to track the actual mesh,because that’s impossible since it doesn’t even have one,but track the pivot point.I noticed i can’t use a SphereCast,because that’s an empty gameobject and it has no collider…
Can i somehow check and add to the array only the gameobjects that are in a certain range?I also said “check” because it would take too long to process adding all my planets from my scene and then check the distance for each of them.Something like a sphereCast but that does not require a collider.Is that possible,or i have to pick a solution from above?Thanks for your anwsers
Uploaded with ImageShack.us
Uploaded with ImageShack.us
What Umbra does is it divides the scene into cells and then builds a map of which cells are visible from any given cell. If you can’t use physics queries or Umbra, maybe you can replicate that system?
how about using the empty gameobjects under the parent gameObject transform called “planetGos” below:
var planetGos : Transform;
var proximity : float;
for (var planetPiece in planetGos )
{
// check if the planet piece is in front of the camera and less than a certain distance
if ( camera.main.InverseTransformPoint( planetPiece.position ).z > 0 Vector3.Distance( camera.main.transform.position, planetPiece.position) < proximity) DoTheThingWhereYouMakeThatPlanetPiece( planetPiece );
}
since there are no meshes, there are no bounding boxes, otherwise you could use
GeometryUtility.TestPlanesAABB
I agree with AngryAnt, you should just partition your scene if needed and attach player (or camera) proximity scripts to those nodes, i.e a script that wakes up every few seconds and checks the distance to the player. If it’s somewhat close, enable game object and it’s children, else shut it down. You can use MonoBehavior.Invoke() since it gets called even if the script’s game object is disabled.
tedchirvasiu07 has already partitioned the scene by placing the empty game objects at the location of every mesh-to-be-created.
My example could then be ‘a script that wakes up every few seconds and checks the distance to the player’ (and also if it is in front of the player), then make the mesh or disable/destroy it. - but it may need to run more often than every few seconds depending on the camera navigation.
Thanks priceap,it seems like the fastest way is the good old way.Also your ideeas,guys,gave me an ideea : I can break my universe into lots of “solar systems” let’s call them.So i will have each planet parented to it’s solar system,then each system parented to it’s galaxy and then each galaxy to the universe,so it won’t have to check trough all the planets and chuncks each second.It will first check in which universe i am ,then it will check in which galaxy i am,then it will check in which solar system i am,then it will activate those planets and when you get close to a planet,it will spawn the chuncks.And this way it should do it
And if i want my planets to actually move and orbit and all that,even if i am not in that solar system or galaxy or whatever,i will just deactivate all the mesh renderers and remove colliders,if they have any.This should do it.Thank you very much for your help,guys