A quick Unity 2D randomized backdrop / space ship hull graphic script

In Space Pillage, various bits of the game are randomly built using modular assets. One of these types are the space ship hulls / interiors you fly through (or over) in the game. This is a quick development update showing how the interior graphics are randomised and created as needed for the game.

These interiors, for now, are purely in for aesthetics and atmosphere. There is no current gameplay usage for them, other than to possibly be part of missions or the story. However, there is a spawning system behind them (like many other assets), that players are able to leverage and modify themselves.
To spawn one of these hulking space ship hulls that fly past as part of the parallax layering system in the game, the modder just needs to add a couple of lines of XML to the level descriptor files in the appropriate place (which determines when these items spawn).

On spawn, the code attached to the template object that is used for a space ship hull item randomises all the middle pieces to create some variation. For now each hull is made from 3 x modular pieces which are randomly placed, with two different end “caps” that don’t move.

I’ve modified the script to run in the Unity Editor, so it is easy to demonstrate. See the below GIF for a low-res demo (Make sure you click the link to view full screen)!

Here is the end part of one randomised space ship hull as it would appear while flying by in-game:

news-2

In terms of code, it’s not that special or pretty either. My excuse is that I wrote it on the train on the way in to London during rush hour last week 🙂 What you do, is:

  • Attach this script to an empty parent gameObject, and then add sprites as children of the gameObject that represent each piece of your full object.
  • Position them with each graphic you want to use as they should be positioned, and ensure you name the gameObjects “Panel1”, “Panel2” etc…
  • Make two end “cap” pieces that will not be moved by the script (name these “LeftCap” and “RightCap” for example).
  • Drag and drop each “movable” child panel sprite gameObject onto the script’s “Object Pieces” IEnumerable (List) field which should at first be empty. This gives the script the items it needs to re-position randomly when the main “parent” gameObject is enabled. (The script only works for the OnEnable method). Make sure you don’t drag your two end “cap” pieces into this field though, as you don’t want those moved.
  • The script is set to “ExecuteInEditMode” so if you enable/disable the script in the editor, you should see your items move around each time.

 

 

That’s it!

Please follow Space Pillage’s development on IndieDB over here for more stuff like this!

Leave a Comment