- Home /
Move all objects in scene (including terrain)
Hi all.
I'm trying to create a function that, when called, creates an array of all objects in the scene, iterates through it, and moves everything to another point (keeping relative positions). I'm almost there, using the code below; it creates a game empty, works out the iteration's position relative to it, and applies that difference to world 0,0,0. This works for all GameObjects:
var gameObjs : GameObject[] = FindObjectsOfType(GameObject) as GameObject[];
var newEmptyGameObject : GameObject = new GameObject("master");
newEmptyGameObject.transform.position = gameObject.transform.position;
for(var thing : GameObject in gameObjs) { var tempV3 = thing.transform.position + newEmptyGameObject.transform.position; thing.transform.position = Vector3(0,0,0) + tempV3; print(thing.name); }
(please excuse the lack of formatting, I'm doing this on an old browser at work...)
My problem is, the terrain doesn't get included in the array, and so isn't moved along with just about everything else. I think particles are also left. I'm guessing the terrain isn't a GameObject, but an Object?
I've tried changing the "GameObject" parts to "Object", but then I can't access the transforms and move everything.
I'm pulling my hair out, can anybody point me in the right direction please?
(p.s I know this is expensive to do; I've omitted an if() statement from the for(thing) part, so only root items are moved).
Cheers
Ian
To stop floating point error. I want to periodically move everyting so the player is approximately at 0,0,0.
Answer by fafase · Oct 18, 2013 at 11:11 AM
Wrap everything into one empty game object except your player.
You can then move that game object around and all will follow (except your player).
Following is irrelevant to the question:
Note that if you need to do so because your player gets too far and you reach floating point inaccuracy (the warning), you may have a level design issue.
That means your level is about 10km long, you may want to cut that into smaller parts (personal point of view).
Fafase- that was the first thing I tried, but I was still struggling to find none gameobjects. As for the level design, it's an open world design, hence the problems. I'll have to break it down into smaller chunks and have loading screens, I suppose. Thanks for your help
Ian
Your answer
