- Home /
Random generated debris in empty space
So I'm building a simple space game. Basically you're able to float around at absolute freedom without any drag or gravity.
Here's how it works:
I use rigidbody.AddForce to propel the player forward.
When player gets further than 100 units from the center of the map, he, and every object is teleported back, seamlessly keeping him in the center of the map to avoid floating instabilities at extreme distances while maintaining the illusion of infinite space.
At the beginning of the level, a lot of debris models with rigidbodies are spawned within a radius of the player.
Certain events in the map delete all current debris floating around, and randomly spawns some new ones in a radius around the player.
The debris is in constant movement, and allows the player to bump into it, changing its and his velocity and rotation.
Here's what I want:
When the player moves into an unexplored area, i want a script to automatically populate that area (within a large radius of the player. 1000 units to be exact) with debris from an array. If the player should return to an already populated area, the script should do nothing.
I thought about using chunks like most games with infinite worlds, but I've got no clue how to implement it.
Note: I write in C#.
Answer by initTechsuport · Mar 26, 2014 at 11:26 PM
the most efficient way would to be randomly generating the debris using a pool system, where you re use the objects you started with, and basically change their position and visibility.
but if your not worried about efficiency, using the chunks through LoadLevelAdditive, then having the new part of the level you loaded moved into place along with the character.
if this system is meant to specifically bypass getting floating point inaccuracy, try having the charachter stationary and only mess with his rotation, while instantiating, moving and destroying the debris instead of moving the character.
also scaling things down and lowering the speed lets the player seem to travel a lot farther while only moving a fraction of the distance.
I don't care about efficiency at this point. As far as I know, LoadLevelAdditive is a pro-only feature which loads an already set up scene without clearing the current scene. This is not what i need, i need to generate debris around the player where there is none. The floating point system works flawlessly and does not need to be tampered with.
sounds like your looking for specific code
using UnityEngine;
using System.Collections;
public class BossAssScript : $$anonymous$$onoBehaviour {
public Transform player;
Vector3 startPos;
float distanceTracker;
public float $$anonymous$$Dist;
public Transform debris;//thing to instantiate(spawn)
public int density;//amount of things to spawn
public float spawnRange$$anonymous$$ultiplyer=.5f;//
void Start(){
if(player){
startPos=player.position;
SpawnStuff();
}
}
void Update(){
if(player){
distanceTracker=(player.position-startPos).magnitude;
if(distanceTracker>=$$anonymous$$Dist){
startPos=player.position;
SpawnStuff();
}
}
}
void SpawnStuff(){
for(int i=0;i<density;i++){
Vector3 randPos=(new Vector3(Random.Range(-$$anonymous$$Dist,$$anonymous$$Dist), Random.Range(-$$anonymous$$Dist,$$anonymous$$Dist), Random.Range(-$$anonymous$$Dist,$$anonymous$$Dist)))*spawnRange$$anonymous$$ultiplyer;
Transform tingy = Object.Instantiate(debris, player.position+randPos, Quaternion.identity) as Transform;
//do other stuff to thingy
}
}
}
Your answer
Follow this Question
Related Questions
Mapping Cube to Sphere - Using 64 planes as a cube "side" 1 Answer
[C#] 2D infinite Terrain Generation with prefabs?! -> lag 0 Answers
How can I go about creating an "Infinate" asteroid field?? 1 Answer
loading planets/ rendering distances (KSP planet) 1 Answer
how can i make an infinite map using tilemap and also making chunks 0 Answers