Search Unity

[WIP] Simple Fence Builder

Discussion in 'Works In Progress - Archive' started by smada-luap, Sep 19, 2015.

  1. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    One of the things that I've always found relatively time consuming was the creation of fences, walls, etc. across the terrain out of prefabs, so I decided to write my own system for creating them for me, and to build in some randomness into it so that they weren't just perfectly straight fences (although if I configure it correctly it will produce straight fences as well :) )

    Having access to the fence pieces in the Village Exteriors Kit from @S4G4N I decided to use them in my intial testing.

    I have the fences 'grow' from an initial anchor point, and the first version gave me some good results (the player character is actually at the end point of the fence here)...

    screenshot1.jpg

    Changing the seed value used to create the fence, I left Unity in play mode and recreated the fence again...

    screenshot2.jpg

    So the randomness was working and it was also picking randomly between numerous prefabs so that I had a mix of long and short fence pieces.

    Once I knew that was working, it was then down to allowing the occasional 'broken' fence to appear, or even gaps along the length of it.

    screenshot6.jpg

    screenshot7.jpg

    As I'm working with prefabs, it also means I'm not restricted to the visual style of the fences I could create...

    screenshot12.jpg

    This last image also shows one other thing that I was glad to have found a solution to and that was one of following the undulations in the terrain which the first version didn't actually deal with.
     
  2. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    It was then yesterday that I decided that if I could use fence prefabs, why couldn't I do the same with walls or hedges? I had these walls so I thought I'd give it a try, which threw up another problem - the fences I'd been using previously were all designed so that the length of the fence was on the Z axis, but the length of the walls I had were designed with their lengths on the X axis which meant I needed a way of letting me tell the system I was building which axis was the primary axis in which the prefab was set in.

    After an evening's coding and a few wrong turns, I got it to work and this was the result...

    screenshot24.jpg

    To show the difference between telling the system whether the primary axis to work with is either the Z or the X, I can better explain with the following two images.

    I've used some of the cave walls from 3DForge which, like the wall prefabs you can see used above, are designed with X as the primary axis. Leaving the system with the default setting of using the Z axis, got me this...

    screenshot22.jpg

    Not really what I was wanting, so changing it to work on the X axis instead gave me this...

    screenshot21.jpg

    There's still a few kinks to work out, but I'm getting there and I'm happy with the results I've been getting so far. :D
     
  3. S4G4N

    S4G4N

    Joined:
    Mar 13, 2013
    Posts:
    3,215
    @smada luap
    I am very impressed and with the excellent results that this project is showing
    I know that many would want to use this solution to assist in automating this task and make it more fun
     
  4. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    Thanks.

    It's certainly fun to play around with it in its current state and see what fences come out - especially as it's now just a few clicks rather than having to position and/or rotate each piece manually. Let's me get results in a minute or two instead of 15-20 minutes :)
     
    S4G4N likes this.
  5. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    Another option that's useful and came at the suggestion of a friend was to have the option of keeping the fence 'growing' in roughly a straight line from the starting point but still allow random variation in each fence piece that gets added.

    Without this option turned on, you could get a fence appearing like this...
    Untitled-2.jpg
    where it's starting to veer off to the left, but what if you wanted the fence to go as straight as it could but wanted little random variations in how the fence was put together (not every fence can be built in a perfect straight line :) )? Then by telling the system to reorient itself back towards the original direction of growth before it creates a new fence piece we can get this...
    Untitled-3.jpg
    From the angle this picture was taken it still looks like it's veering off to the left, which it is to a minor extent but the terminating post of the fence isn't too far away from where the post would be if it was perfectly straight.
     
  6. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    S4G4N likes this.
  7. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    I am using this. The first couple of images showed that I could recreate random fences even in play mode and it wasn't fixed to just in the editor.

    In fact, using the executeineditmode option allows me to have a 'live update' when I move or rotate the anchor object and see it change to reflect the environment
     
    S4G4N likes this.
  8. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,038
    This is the sort of thing I always thought should have been core functionality in game engines, but until it is, please make it available to the rest of us somehow :)
     
    S4G4N likes this.
  9. S4G4N

    S4G4N

    Joined:
    Mar 13, 2013
    Posts:
    3,215
    @orb I can agree with that, but that is how many small "Gems" are born on the Asset Store :);)
     
  10. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    It's something that I'm considering releasing to the asset store, but as this would be my first foray into this area, I'm trying to make sure that what it does, it does to the best of my (current) ability and that the various settings are easily understood.
     
    S4G4N, BackwoodsGaming and orb like this.
  11. BackwoodsGaming

    BackwoodsGaming

    Joined:
    Jan 2, 2014
    Posts:
    2,229
    Looking great! Love that you have shown it with different fences, rock walls, etc.. Looking to have a lot of potential! Keep up the awesome work!
     
    S4G4N likes this.
  12. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    I seem to have a lot of fence pieces to experiment with out of the Village Exteriors Kit, so that's where the name originally came from, but there's no reason why it can't work with any other prefab'd object :)
     
    S4G4N likes this.
  13. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,038
    Maybe call it Smada Luap's Extreme Prefab Connection Tool, or something.
     
    S4G4N likes this.
  14. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    lol.

    I think I may fall back on something like that if I have to :D
     
  15. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    Really pleased this evening as I've extended the capabilities so that you can make it build a continuous fence that could stretch around a building, whilst maintaining all the little random elements that you want along the way...

    screenshot36.jpg
     
    S4G4N, John-G and BackwoodsGaming like this.
  16. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    It's quite amazing how many inadvertent code bugs you can create and logic inconsistencies that appear when you take a different approach to what you did before.

    Using what I added previously, I tried another test by making it used a preset pattern of fence pieces (a 1m piece, then 2 x 2m pieces, and then a 1m piece) which the system will use and loop back to the beginning each time until it reaches the end of what it's told to build.

    I then applied some design rules that basically told it "After 1m of fencing, turn left by 45 degrees. After another 1m of fencing, turn right by 45 degrees. After another 1m of fencing, turn right 45 degrees again, and finally after another 1m, turn left by 45 degrees again."

    So, given the pattern of fences that I wanted to build and the rules I'd set it up, it gave me this...

    screenshot37.jpg

    What's great about this is that when it came to put in the first of the 2m fence pieces, it knew this was longer than the defined length of the second piece of fencing and thus had to make the next fence piece turn 45 degrees to the right. The same rule applied to the second 2m fence piece as well. (remember, all sub-lengths of fencing were set at 1m)

    Quickly resetting the pattern to replace the first 2m piece with a 1m piece and recreating the fence gave me this (which still followed the same rules)...

    screenshot38.jpg

    It's getting really close now to where I'm happy with this and ready to submit it to the asset store :D

    (Looks like I know what I may be doing over the weekend :) -- preparing screenshots, etc.)
     
    S4G4N and John-G like this.
  17. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    Further enhanced last night's feature so that it's now an option as to whether or not it should repeat all the rules governing the extra turns, etc. over the whole length of the fence or whether it should be a one-shot deal (such as the original example pic with the fence going around the house)

    So you can now configure it to get fences such as these...

    screenshot39.jpg

    screenshot41.jpg
     
    BackwoodsGaming likes this.
  18. Archania

    Archania

    Joined:
    Aug 27, 2010
    Posts:
    1,662
    Looking really good.
     
  19. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    I like this. As is often the case, I've never done anything where I've needed it, but it looks crazy fun to play with. Thanks for sharing, and keep up the good work!
     
  20. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    As the scene view auto updates whenever you change any of the configuration settings, I've spent longer than I really should have just seeing what effect changing one option has in the overall layout of the fence.

    As I've also made it seed-based, if you find something that gives you a fence that you like, you can then just create a new prefab of this (and thus save all settings for future use), or you can take a note of what you've done (including the seed value) and then if you re-enter those at a later date you'll then get back exactly the same fence :)
     
    Schneider21 likes this.
  21. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    Had some more fun tonight after a session of general code tidying up :)

    I think whoever built this particular fence was drunk at the time :D

    screenshot42.jpg

    screenshot43.jpg
     
  22. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,038
    You know how it is: You start building, it gets hot, you get something to drink, and before you know what happened you both no longer care about the heat OR where you put the fence.
     
  23. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    @orb definitely don't accept the lowest bidder :D
     
  24. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    Quick demonstration of some of the features :)

     
  25. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    I'm very close to submitting this to the asset store for approval. Although I'm glad I hadn't already submitted it as one of my friends who is beta testing this for me (coming up with combinations of ways to use this that I haven't thought of or even anticipated) spotted a rather serious error in the code for dealing with fences and how they should sit depending upon the terrain.

    Suffice it to say nobody would want what I originally had the code producing, but here's a pic of the final results after tracking down the bugs and eliminating them :)

    screenshot51.jpg

    All the fence pieces here now correctly follow the slope of the terrain.
     
    Haagndaaz, BackwoodsGaming and orb like this.
  26. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    Well... that's it now... I've submitted it to the Asset Store for approval, but this doesn't mean I'm stopping development as I'm sure I'll get other ideas to extend this or people will offer some to me :)
     
    BackwoodsGaming likes this.
  27. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    Would just like to show that even though it says 'Fence' builder, you're not restricted to just fence prefabs :)

    screenshot52.jpg

    These two rows of bushes are placed using the code :)
     
    BackwoodsGaming likes this.
  28. BackwoodsGaming

    BackwoodsGaming

    Joined:
    Jan 2, 2014
    Posts:
    2,229
    Hmmm.. Wonder how it would do assembling a small outpost.. Random 3DForge exterior kit houses (after defining in list) along a road.. Make a prefab that includes your building and any outside props for that particular building. Hmmm... The ideas are rolling atm!
     
  29. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    Never thought as big as houses to use as objects :) Some other ideas to consider too :)
     
  30. BackwoodsGaming

    BackwoodsGaming

    Joined:
    Jan 2, 2014
    Posts:
    2,229
    Maybe something to save for a sister project. I think you have solved a lot of the placement issues with the fence system. Maybe recycle some of the same code and add things that would deal with building placement that aren't needed with something as simple as fences. And maybe give it options to place in a row or a grid. (single along the road outpost, or a complete village) Anyways, some food for thought. Here is wishing for quick approval and much success on the simple fence builder asset!
     
  31. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    Fed a house prefab'd object into the system and tested the outcome.

    It's also given me some ideas as to what can be included in a 1.1 update :D

    screenshot53.jpg
     
    BackwoodsGaming likes this.
  32. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    Also working on an improved visual interface for when you're defining what you're wanting to make your fence out of (or wall, or row of houses/bushes/trees/etc.) - the biggest of which is a little preview image of what you're wanting to use.

    There's other options that I'm still considering and all will go in future updates.

    Untitled-3.jpg
     
    manpower13 and BackwoodsGaming like this.
  33. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    Still working on a few things, but I've had one breakthrough today :)

    Even though it's called Simple Fence Builder, I've been testing it lately with other prefab'd game objects. In the image below, I've been able to make it so that you can tell it how many of each particular item can appear, and in this instance I've set it so that the second house in this row (from the left) should only appear once.

    screenshot57.jpg

    Under different seed values, this house could appear anywhere in any of the 5 positions, but it will only appear the once.

    If I take the restriction off, so everything has an equal chance of appearing then under the same seed value that created the image above, I got this...

    screenshot58.jpg
     
  34. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    You can even have a repeating pattern of tree, house, tree, house, etc. if you want :D

    screenshot59.jpg
     
    S4G4N likes this.
  35. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    Made some improvements to the custom inspector for the prefabs, etc. that you can use with this :)

    Untitled-2.jpg


    Just to round off this post with a new screenshot utilising the prefabs above :)

    screenshot60.jpg
     
    S4G4N and BackwoodsGaming like this.
  36. ironbellystudios

    ironbellystudios

    Joined:
    Jul 21, 2015
    Posts:
    410
    One item/question that may have been answered but I missed it- is there a way to ensure a height value (minimum) of the fence? In a lot of games fences are used to bound areas, but you can't have a fence (due to terrain, randomness etc) piece be 'jumpable' - so you'd need to ensure they all are at least Y height.
     
    S4G4N likes this.
  37. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    This works purely off the gameobjects that you feed into it. If you're wanting a fence that isn't 'jumpable' then you could set the collider on the object to be higher than the actual height of the mesh.
     
    S4G4N likes this.
  38. GameTechnix

    GameTechnix

    Joined:
    Oct 5, 2015
    Posts:
    84
    Looks great, can't wait to give this one a go with some stone walls.
     
    S4G4N likes this.
  39. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    It started out in development as just a way to build fences, but then a friend asked me if it could build walls or hedges, and I replied that I couldn't see why it couldn't. It was then that I tried experimenting with it and realised that extra options would be required to facilitate this (which is where the Primary Axis really came from as the wall prefabs that I had were designed off a different axis to the fence pieces I had)
     
    manpower13, BackwoodsGaming and S4G4N like this.
  40. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945
    This is now available on the asset store at http://u3d.as/k1j

    There's some other additions that I'd like to get into this if I can so this is still a wip for now :)
     
    S4G4N and BackwoodsGaming like this.
  41. S4G4N

    S4G4N

    Joined:
    Mar 13, 2013
    Posts:
    3,215
    Great, picked it up !!!
     
  42. florianalexandru05

    florianalexandru05

    Joined:
    Mar 31, 2014
    Posts:
    1,813
    That's a kinda cool idea! ;)
     
  43. smada-luap

    smada-luap

    Joined:
    Aug 22, 2013
    Posts:
    945