Search Unity

Endless - 2D Terrain Generator

Discussion in 'Assets and Asset Store' started by NeilMeredith, Oct 25, 2013.

  1. NeilMeredith

    NeilMeredith

    Joined:
    Jul 15, 2013
    Posts:
    81
    This asset is now free. I no longer have time to support that asset, but hope someone finds the source code useful. Enjoy!

    For anyone interested in my other projects, you should check out TrueNovelist, a site to help you organize and structure a novel. It could also be used to help you structure game notes, background info, etc.


    I got started with Unity a few months ago, and decided I want to make a game similar to Hill Climb Racing for android. The problem was, I couldn't find any code anywhere that would let me dynamically generate hills for an infinite runner. If I could have found an asset to do that, I would have bought it...but since I couldn't, I decided to create my own.

    Here it is! Endless - 2D Terrain Generator in Asset Store

    $endless.jpg

    Features
    • Dynamically generated terrain - can easily change look and feel for rapid prototyping
    • Full C# source code included
    • Can generate terrain at an angle or in a straight line
    • Rule based terrain generation and prefab placement - no coding required
    • Mobile ready! Tested on HTC EVO 3D for android and keeps scrolling smoothly
    • Works with orthographic or perspective cameras, for a 2D or 2.5D look


    Demo


    Quickstart - Covers more of the setup details


    Thanks for checking it out!
     
    Last edited: Jul 15, 2015
  2. NeilMeredith

    NeilMeredith

    Joined:
    Jul 15, 2013
    Posts:
    81
    Now that a few people have purchased this asset, I'd love to hear what you are using it for. Feel free to post links to your projects/prototypes/etc. that you are planning on using this for!
     
  3. NeilMeredith

    NeilMeredith

    Joined:
    Jul 15, 2013
    Posts:
    81
    I have created a limited trial version for anyone who wants to try this before they buy.

    It has all the features of the full version, with a few limitations:

    o No source code.
    o Terrain only generated for a limited distance.
    o Watermark "Trial" button in upper left hand corner.

    Enjoy!
     
    Last edited: Nov 6, 2013
  4. ProjectOne

    ProjectOne

    Joined:
    Aug 9, 2010
    Posts:
    442
    Hi,
    is it easy to change scrolling to have the camera and player-character always at the centre of the screen (instead having camera and player always scrolling away from the centre of the Unity Grid). Camera at the centre and terrain scrolls left/right... or would this change upset functionality of your asset somehow?

    Thanks
     
  5. NeilMeredith

    NeilMeredith

    Joined:
    Jul 15, 2013
    Posts:
    81
    This would be difficult to change. I can look into it, but right now the terrain generation is based around the x location of the camera and the assumption that the camera always moves to the right, so adjusting it to keep the camera in one spot and scroll the terrain would take a significant amount of time.
     
  6. ProjectOne

    ProjectOne

    Joined:
    Aug 9, 2010
    Posts:
    442
    Right,
    because I am worried about issues mentioned in other forums on similar subject where the player moves far away from the centre of the coordinate system and precision is lost, I cannot remember the details but I remember there could be issues... unless this is an old problem solved in latest versions of unity or a problem that would only occur so far away from the centre of the coordinate system that the player would actually never get there.

    If I am talking about a no issue sorry I don't mean to disturb your thread
    Of course if you could add to your package the option of scrolling either left or right during gameplay and keep camera centred it would be cool features to have anyway that should add value to your asset anyway (I think)

    Thanks
     
  7. NeilMeredith

    NeilMeredith

    Joined:
    Jul 15, 2013
    Posts:
    81
    You're right, it is a potential problem. I will look into moving everything back to the origin every couple thousand units, which should be easier than keeping the terrain constantly scrolling underfoot, and should also fix the problem. I'll let you know if I am able to get that working and update the demo example so you can try it out.
     
  8. ProjectOne

    ProjectOne

    Joined:
    Aug 9, 2010
    Posts:
    442
    Excellent yes that would be an efficient solution

    Your asset is on my buy list will get it soon for my next endless runner project :)
     
  9. D3k2

    D3k2

    Joined:
    Oct 25, 2013
    Posts:
    5
    Hi, I was just wondering if there was a way to essentially turn some prefab rules on and off after a certain length of mesh has been generated. I'm using the prefab rules to generate enemies for the player and want to turn them off for a boss fight I want in the same scene. Would this be possible or would I have to find a work around.
     
  10. NeilMeredith

    NeilMeredith

    Joined:
    Jul 15, 2013
    Posts:
    81
    I am actually just getting ready to submit an update to the asset store that allows you to limit prefabs to a certain distance or a certain terrain generation rule. It usually takes about a week to get an update approved by the asset store though...if you already bought the asset, send me your invoice number and I can email you an advance copy with the changes.
     
  11. D3k2

    D3k2

    Joined:
    Oct 25, 2013
    Posts:
    5
    Wow, thanks for the quick update. I went ahead and PM'ed you my invoice number.
     
  12. ProjectOne

    ProjectOne

    Joined:
    Aug 9, 2010
    Posts:
    442
    Hi Neil
    any update on Moving camera/scene back to centre of grid automatically?
    or on scrolling while camera/player remain at centre of screen?

    Thanks
     
  13. NeilMeredith

    NeilMeredith

    Joined:
    Jul 15, 2013
    Posts:
    81
    No, I'm afraid not. I've been pretty busy at work and haven't gotten much a chance to work on this, especially since it is such a large change. Once I do get started on it I'll let you know though.
     
  14. ProjectOne

    ProjectOne

    Joined:
    Aug 9, 2010
    Posts:
    442
    ok thanks
     
  15. EddieChristian

    EddieChristian

    Joined:
    Oct 9, 2012
    Posts:
    725
    Not sure if the new version is out but i am going to PM you my invoice as well :)
     
  16. NeilMeredith

    NeilMeredith

    Joined:
    Jul 15, 2013
    Posts:
    81
    Yes, the new version (1.1) is out, so if you update to the latest version you should get those features.
     
  17. NeilMeredith

    NeilMeredith

    Joined:
    Jul 15, 2013
    Posts:
    81
    @ProjectOne - Just so you know, I am working on the terrain shifting back to the origin. I have able to get the terrain itself to shift automatically, but after moving the prefabs it is completely messing up my prefab generation rules. I'm not sure what an ETA on this would be since if I don't get it done this week I will be out of town for several weeks on a work trip, but I'll keep you updated.
     
  18. ProjectOne

    ProjectOne

    Joined:
    Aug 9, 2010
    Posts:
    442
    sure, thank you for the update
     
  19. D3k2

    D3k2

    Joined:
    Oct 25, 2013
    Posts:
    5
    Hi again. I've tried out your latest version and its working great for me. What I'm wondering now is if its possible to have multiple prefabs in the same rule and have that rule be able to switch between the prefabs. What I'm trying to do is have different enemy types within a level and not have them overlap on each other, since they do different things to the player. The only thing I can think of is create a long list of prefab rules that switches prefabs based on the distance, but I'm hoping there's an easier way to do this.
     
  20. NeilMeredith

    NeilMeredith

    Joined:
    Jul 15, 2013
    Posts:
    81
    That isn't something that is supported. If you don't mind adjusting the code a bit, you could setup two rules for the prefabs (say one for every 5 units and then one for every 10).

    Then in the prefab manager class, before you instantiate the prefab you could search for all other game objects nearby and if there are any too close then don't instantiate the prefab (but still update the rule.lastprefablocation). That would prevent you from placing overlapping prefabs.

    Code (csharp):
    1.               if (pq.angle >= rule.MinSlope  pq.angle <= rule.MaxSlope  allowedForThisTerrainRule  meetsDistanceReqs)
    2.                                 {
    3.                                     //Check here to see if there are any other prefabs of x type within a given distance
    4.                                     //If so, don't instantiate the prefab (but do still update the rule.lastprefablocation)
    5.                                     rule.InstantiatePrefab(pq.location, PrefabManagerObject, Pool, pq.angle);
    6.                                     rule.LastPrefabLocation = pq.location;
    7.                                 }
    8.  
    Hope that helps.
     
  21. D3k2

    D3k2

    Joined:
    Oct 25, 2013
    Posts:
    5
    I went ahead and looked into the prefab manager class and the code you provided seems to be the same as what's already in the class. Is this supposed to be the case?
     
  22. NeilMeredith

    NeilMeredith

    Joined:
    Jul 15, 2013
    Posts:
    81
    Yes, I was just showing where you would have to add new code to handle the case you described.
     
  23. D3k2

    D3k2

    Joined:
    Oct 25, 2013
    Posts:
    5
    Oh okay. I'll go ahead and tinker with it then. Thanks for the help.
     
  24. EddieChristian

    EddieChristian

    Joined:
    Oct 9, 2012
    Posts:
    725
    I can only go Right????? Really??? Please tell me this is an old Manual I am reading, Cause I need to go Left and Right.:(

    By Tomorrow for a client presentation.
     
    Last edited: Nov 22, 2013
  25. NeilMeredith

    NeilMeredith

    Joined:
    Jul 15, 2013
    Posts:
    81
    Sorry SoloChristian, just saw this. I must have gotten unsubscribed from this thread. Yes, right now you can only go in one direction, and the default direction I chose was to the right because that is the usual for a side scroller. You can adjust your character controller to allow the user to go left as well, but you will have to keep them from going too far left because the terrain is cleaned up behind you.
     
  26. EddieChristian

    EddieChristian

    Joined:
    Oct 9, 2012
    Posts:
    725
    Is there any future Updates or Jerry Rigs that can be had to make it Bi-Directional. I Really Really need that functionality.
     
  27. NeilMeredith

    NeilMeredith

    Joined:
    Jul 15, 2013
    Posts:
    81
    Just to be clear - when you say bi-directional, do you mean you want it to be going to the right, then you want to have some event that flips the direction and sends you to the left? Or do you want to just be able to choose at the beginning if you are generating the code going to the right or going to the left?

    If you want to be able to change the direction halfway through, then no I don't really have any plans for that. If you just want to choose if the code starts generating to the left or to the right, then possibly...I'd have to go through all the code and swap out everything that is hard coded going to the right with a variable to let you choose.

    Either way, I wouldn't be able to get to it that soon, I am out of the country for work. If you bought this under the assumption it could go in both directions and it doesn't meet your needs because of it, you can send me an invoice number and I will give you a refund for the asset.
     
  28. OakNinja

    OakNinja

    Joined:
    Dec 9, 2013
    Posts:
    2
    I tried your demo release and I think it's great. However, collisions won't work for me. With 2d collision components, i get no success at all, everything falls right through. With 3d collision components, my test box hit the surface but still falls through in the valleys. Any ideas?
     
  29. NeilMeredith

    NeilMeredith

    Joined:
    Jul 15, 2013
    Posts:
    81
    I actually haven't tried this with the new 2d collision components, although I assume they would work since the terrain is just a standard mesh collider. 3d components should work fine.

    I would check to be sure your 3d component is set to the same z index as your terrain. Also try looking at the constraints of your rigidbody for the collision object, and freeze the position in the z axis and the rotation in the x and y axis to make sure your component isn't rolling off the mesh.
     
  30. yowchun

    yowchun

    Joined:
    Jul 29, 2013
    Posts:
    3
    Hi Neil, i just bought the asset tool. I would like to ask at which script does it create new terrain and destroy the old terrains when it is out of the camera view. Thanks
     
  31. NeilMeredith

    NeilMeredith

    Joined:
    Jul 15, 2013
    Posts:
    81
    Thanks for the purchase! The terrain is created and destroyed in the GenerateTerrain method of the TerrainDisplayer class
     
  32. avuthless

    avuthless

    Joined:
    Dec 4, 2012
    Posts:
    6
    Hi Neil.

    I just purchased your asset and only after noticing some issues did i see your demo in this thread. Well i guess i will have to wait for updates then. Some issues that i noticed are:

    When you go far away from center, objects are created with bad precision and therefore they are rotated or positioned bad and if distance gets really great, the items go off quite a lot. (I know this issue is known, just wanted to confirm this).

    Next issue i noticed is that objects are loaded with delay in time. Since i am creating 3D objects as obstacles in your generator, some objects get created in the middle of scene (maybe load-up of objects takes too long to be loaded in time) i think this could be fixed by implementing object pool, which would reposition old objects instead of creating new ones. (Did not dive into the code, maybe some pool system exists and maybe this object creation mismatch is just another case of position mismatch on far from zero cases). Will report after more tests.

    Also mentioned issue is that objects are created on top of each other. If i set collected items (coins, hearts, potions), sometimes they are created so, that they collide. Maybe some "Item object list" (as offered by D3K2), or some "generation layer" would be a nice so that they would not get created on top of each other.

    I did not test on mobile devices and i mainly wanted terrain creator part so i cannot complain much about these issues and i cannot say if this asset fits my needs. But only issue i seen with generated terrain is "flickering" border of top layer (the pavement). To be more exact, i attached a photo. Would be nice if top layer (the road) could be generated wider than the ground itself.

    $Screenshot 2013-12-29 00.13.43.png

    My overall evaluation would be quite positive. Your terrain generator looks promising, so none of my issues should define your work as bad. These are minor issues and if fixed, your asset will be really great one! So i wish you all the luck in making this asset better.
     
  33. NeilMeredith

    NeilMeredith

    Joined:
    Jul 15, 2013
    Posts:
    81
    Avuthless,

    Thanks for your purchase!

    In response to your comments:

    1. Yes, I do still need to work on the issue when items are far from the center. It has proven more difficult than I thought to move this back to the center, and I haven't had much time because of work. Still looking into this.

    2. There is an existing object pool...I think what is probably happening here is that I only generate the terrain/objects every .5 seconds, since in my demo there is no need to go faster. However, if you change this line of code

    Code (csharp):
    1.        yield return new WaitForSeconds(.5f);
    in the GenerateTerrainCoroutine method of the TerrainDisplayer class, you can decrease the generation time, which may take care of the issue for you.

    3. Yes, right now the generation rules are all separate from each other, so it is possible items will be created one atop another. You can control the layering with the z-index. In some cases this is good (like for background items), but I can see where it would be an issue in other cases.

    What exactly would you want to happen in this case...if something is already there just don't generate any other objects there?

    4. You can adjust the width of the road (in the Top - Height) setting. Also, although you can see some flickering in the scene view when you actually run the game in the game view or when deployed to a mobile device you should not see any flickering.

    -Neil

     
  34. avuthless

    avuthless

    Joined:
    Dec 4, 2012
    Posts:
    6
    Hi again!

    Well i am glad to hear that you will look into this "imprecision on distance" issue.

    I do not move very fast for creation delay to be of any issue. I will investigate this later and report back. As i mentioned before, this may be just another case of precision mismatch. In such case, the item is created too close (already in-screen, instead of somewhere further ahead).

    What i did want to create is something like this: Player has not only coins as collectibles, but also more valuable coins, collectible objects and/or specials. All these items are different prefabs and have different creation rules. Even though coins are created often, special item should be created 10x times less often. This would require not just "many prefabs selected randomly" (ability to define many prefabs for one rule) but some "item rule grouping" (which would be aware of all items created in this group and avoid creating items on existing ones) or "aware item creation" (that would check with any other created item) that would detect any items created on the same point. Scene objects and coins work great with your current implementation, but if you would add obstacles (like 5 types of obstacles), then your generator would generate coins and obstacles everything mixed together on top of each other (not always). Hope you see my point.

    The image i provided with flickering side of the road is made in game scene. I did not try that on device yet so i do not know if that stays so. Yes, you are right, road width can be modified, but this modifies not only "road" (the top part of scene) but the whole scene width too (it would be nice if you could have different widths for both parts of scene).

    My team is currently busy on other projects so i cannot be of any help here right now. Neither do i need any of these bugs fixed ASAP. I should get some extra time next week, then i will try playing with this tool a bit. Thanks for fast response!
     
  35. Avinash-pdy

    Avinash-pdy

    Joined:
    Mar 4, 2013
    Posts:
    28
    Hi,

    I just integrated this and tried with a sample game. I am noticing a flicker on grass layer when running on any iOS device (iPhone 5S, iPad 2). Do ia need to change some setting to build it on device.
     
  36. NeilMeredith

    NeilMeredith

    Joined:
    Jul 15, 2013
    Posts:
    81
    Avinash.pdy,

    I've only tested this on Android, where I do not see any grass flicker on any of the sample scenes. I just got a Mac for Christmas though, so I'll have to get my developer account setup to see if there is a mac specific issue. Can you tell me which sample scene you are seeing the grass flicker in?
     
  37. Avinash-pdy

    Avinash-pdy

    Joined:
    Mar 4, 2013
    Posts:
    28
    Its happening in OrthographicCamera. I didn't test other scenes though. Let me know if you need me to change something in project. I am also looking for the reason. Will post if i find it.
     
  38. avuthless

    avuthless

    Joined:
    Dec 4, 2012
    Posts:
    6
    Thats the flicker i was talking about too. It exists on MacBook Pro with Unity 4.3, perspective camera demo scene.
     
  39. avuthless

    avuthless

    Joined:
    Dec 4, 2012
    Posts:
    6
    The reason for flickering is overlapping planes of top and bottom meshes.
     
  40. NeilMeredith

    NeilMeredith

    Joined:
    Jul 15, 2013
    Posts:
    81
    Ok. On android at least I am able to get around that by setting the renderQueue order for the top and bottom meshes to consistently draw them in the order I want. Maybe that isn't working for iOS. Once I get my developer account setup I will test it out.
     
  41. NeilMeredith

    NeilMeredith

    Joined:
    Jul 15, 2013
    Posts:
    81
    Avuthless,

    One other thing - can you tell me where you start running into precision issues? I let the game run for about half an hour, and the character position is at about 25,000 x, but the precision still looks fine. I just want to see if it is an issue with the distance from the origin, or if perhaps it has something to do with the 3d models you are using since everything I tested with was a 2d sprite. Thanks.
     
  42. avuthless

    avuthless

    Joined:
    Dec 4, 2012
    Posts:
    6
    Yes i guess you are right. I start noticing poorly positioned objects from 1000 already. Only object with collider and rigidbody does get created strangely though. Maybe thats some collision issues. Did not test any further.
     
  43. NeilMeredith

    NeilMeredith

    Joined:
    Jul 15, 2013
    Posts:
    81
    Yeah, I think this is something else. I got up to about 100,000 from the x coordinate and it still looks ok. If you can send me a sample of your project I can take a quick look to see if I can figure out what is going on.
     
  44. NeilMeredith

    NeilMeredith

    Joined:
    Jul 15, 2013
    Posts:
    81
    I was able to reproduce the flickering problem on an iPad...I assumed it would work the same as on android, but it looks like the render queue works differently. In any case, to fix the problem go to line 92 in the TerrainDisplayer class and replace this:

    Code (csharp):
    1. s.DetailPlaneOffset = DetailPlaneOffset;
    with this:

    Code (csharp):
    1. s.DetailPlaneOffset = new Vector3(0,.1f,-.2f);;
    This will force the detail plane to move a little in front of and a little above the plane behind it, which eliminated the flickering for me.

    I'll publish this fix in a release, but it will take a week or so before it is live.
     
  45. rtumelty

    rtumelty

    Joined:
    Dec 11, 2011
    Posts:
    12
    Hi Neil, I have been developing a similar tool for a project I'm working on, and just found your plugin. I am considering switching to it to save some time. The only thing that's holding me back is the lack of support for Unity 4.3's 2d physics.

    If I buy the plugin, will I have free access to the source? I'd like to add an option to generate a Polygon Collider rather than a Mesh Collider.

    Cheers.
     
  46. NeilMeredith

    NeilMeredith

    Joined:
    Jul 15, 2013
    Posts:
    81
    Ronan,

    The plugin does come with the full source. That said, I'm not sure how difficult it would be to switch over to the 2D polygon collider, since everything is based around mesh colliders.

    If you do purchase the asset and it looks like the changes will be too extensive, just email me and I can refund your money if you can't use the asset.
     
  47. rtumelty

    rtumelty

    Joined:
    Dec 11, 2011
    Posts:
    12
    Cheers for that!

    I think it should be possible - I'm hoping to take the coordinates used to generate the mesh collider, but instead use their XY in PolygonCollider2D.SetPath. I'll let you know how it goes!
     
  48. ImpossibleRobert

    ImpossibleRobert

    Joined:
    Oct 10, 2013
    Posts:
    527
    I want to use the terrain with a fixed camera and instead of moving the camera I want to move the terrain programatically. Is this also supported? The terrain will be moving to the left and as soon as it would reach it's limit a new piece should be generated. This is IMO quite similar to the current way but would mean that the original position of the terrain would have to be taken into consideration when calculating the needed areas. Would be great to have that!
     
    Last edited: Jan 5, 2014
  49. ImpossibleRobert

    ImpossibleRobert

    Joined:
    Oct 10, 2013
    Posts:
    527
    It would be quite handy to allow an optional selection of the camera if not always main should be used. Hint: if there is no camera tagged main camera the current script will fail with an exception. It took me a while to figure out the root cause :) Would have been nice if the script caught that.
     
  50. ImpossibleRobert

    ImpossibleRobert

    Joined:
    Oct 10, 2013
    Posts:
    527
    It would be great if the terrain could be rotated. I understand that horizontally it probably needs to be perpendicular to the camera, that is ok. But it would be nice if rotation toward the camera (around x) works, so that more of the top layer can be seen without having to move and rotate the camera. Currently the rotation will be reset as soon as a parameter is changed.