unity3d 2dtools - Make an endless 2D map in Unity -
basically, idea game this: have rocket launch ground, , keep adding speed. rises higher , higher, want camera follow rocket, , can move rocket pick special objects , avoid obstacles. how create endless map in unity, rocket won't fly off map. thank you!
// singleton. translates existing obstacles , creates additional obstacles.
// world moves around player.
// when object collided into, level pieces deactivated. // level ends when level pieces deactivated
public class leveltranslator : monobehaviour { public static leveltranslator instance = null;
// fields public bool automaticallystart = false; public bool isendless = false; // of level pieces used? public bool levelover { { if (alllevelobjects.count == 0) return true; else return false; } } // of transforms attached public transform levelparent = null; // prefab add level public list<transform> endlesslevelobject_prefabs = null; // level starts. endless! public vector3 levelobjectorigin = new vector3(); // last created object. endless! public transform lastcreatedobject = null; // contains of transforms level public list<transform> alllevelobjects = new list<transform>(); // game values public float speed = 5; public float objectspacing = 5; // endless. how object spacing there between current level piece , going-to-spawn level piece public vector3 direction = vector3.forward; public bool stopmoving = false; // starts level public void beginmovinglevel() { startcoroutine(beginmovinglevel_coroutine()); } ienumerator beginmovinglevel_coroutine() { while (!levelover) { if (stopmoving) { yield return new waitforseconds(time.deltatime); continue; } // moves transform parent. objects attached parent, it's moving objects... levelparent.translate(speed * direction * time.deltatime); yield return new waitforseconds(time.deltatime); if (isendless) { if (vector3.distance(lastcreatedobject.position, levelobjectorigin) >= objectspacing + time.deltatime) { transform t = instantiate(endlesslevelobject_prefabs[random.range(0, endlesslevelobject_prefabs.count)], levelobjectorigin, quaternion.identity) transform; t.parent = levelparent; lastcreatedobject = t; alllevelobjects.add(t); } } else { if (vector3.distance(lastcreatedobject.position, levelobjectorigin) >= objectspacing + time.deltatime) { transform t = alllevelobjects[0]; t.position = levelobjectorigin; t.rotation = quaternion.identity; t.parent = levelparent; t.gameobject.setactive(true); lastcreatedobject = t; alllevelobjects.remove(t); } } } } void start() { if (instance != null && instance != this) destroy(gameobject); instance = this; if (isendless) { if (lastcreatedobject == null) { debug.logerror("error! it's endless mode, didn't assign lastcreatedobject starting object! fucked up! (well, it's okay, did...)"); return; } alllevelobjects.add(lastcreatedobject); levelobjectorigin = lastcreatedobject.position; } else { if (alllevelobjects.count > 0) debug.logwarning("warning! there no objects in alllevelobjects list! place there in order avoid error!"); lastcreatedobject = alllevelobjects[0]; alllevelobjects.remove(lastcreatedobject); levelobjectorigin = lastcreatedobject.position; } // apply of transforms parent if (automaticallystart) beginmovinglevel(); } // whenever obstacle collides this... deactivate colliding object. void oncollisionenter(collision c) { transform obstaclet = c.transform; if (alllevelobjects.contains(obstaclet)) { alllevelobjects.remove(obstaclet); // "destroys" object obstaclet.gameobject.setactive(false); } }
}
i used awhile ago.
levelparent gameobject. make empty go. endlesslevelobject_prefabs... put in level pieces want spawn
lastcreatedobject. set first level piece.
Comments
Post a Comment