Unity Learning Journey #4 – Building Infinite Runner games, Tips and Tricks

As always, building your environment with level arrangement involves properly arranging your SORTING LAYERS:

  • Background layer, where assets in the background are placed.
  • Character layer, where your playable, moving characters live.
  • Foreground layer, where doorways and assets that your runs through and
    behind are placed.
  • Default layer automatically generated by the program.

Here is how to add an animation using Unity after you’ve imported the assets:

  • Locate the animation frame files somewhere in your Assets folder.
  • Select all of the frames.
  • Drag and drop the frame files into either the Scene panel or the Hierarchy panel.
  • In the window that pops up, give the animation a name. (Run, jump, fall, rotation, idle, death, etc.)
  • This automatically generates an animation and an animator.

Want physics, gravity, running forward, jumping upward force?

  • Select your new animation in the Hierarchy panel (it’s named after the style of your animation frame file names, like Running_Man001. You may want to rename it to “Character” or something similar.)
  • Click “Add Component” –> Physics 2D –> Rigidbody 2D.
    • A Rigidbody 2D component places an object under the control of Unity’s physics engine. In 2D, objects can only move in the XY plane and can only rotate on an axis perpendicular to that plane.
    • Adding a Rigidbody 2D allows a sprite to be moved in a physically convincing way by applying forces from the scripting API. When the appropriate collider component is also attached to the sprite object, it will be affected by collisions with other moving objects. Using physics simplifies many common gameplay mechanics and allows for realistic, emergent behaviour with minimal coding.
  • Click “Add Component” on your character again, –> Physics 2D –> Box Collider.
  • Select the floor of the environment you built, Add Component –> Physics 2D –> Box Collider, and adjust the size and shape of this new box collider to represent the floor of your background layer level prefab. If you do not have colliders on your character and the floor, your character will fall forever.
  • If your character tumbles during gameplay, you need to check the Fixed Angle checkbox.

Here is a plain-English description of a script written to apply a force to a character moving forward in the horizontal axis.

  1. On collision, enter 2D. A function triggered when a collision between the colliders happens.
  2. When the character’s collider hits the ground, grounded = true.
  3. Function Update runs every frame, FixedUpdate runs at a certain interval that we can set.
  4. void Update() { if(Input.GetButtonDown(“Fire1”)) { if(grounded == true) { jump = true; grounded = false; anim.SetTrigger(“Jump”);} } } This function triggers at the press of a button to make the character jump. (Fire1 is an internal Unity command label for any button input.)
    • Edit –> Project Settings –> Input –> Axes –> Fire1
  5. void OnCollisionEnter2D(Collision2D hit) { grounded = true; print (“isground”); } Makes it so that you only jump when your character is on the ground.
  6. void FixedUpdate() { rigidbody2D.velocity = new Vector2(movementSpeed, rigidbody2D.velocity.y); if(jump == true) {rigidbody2D.AddForce(new Vector2(0f, jumpForce).ForceMode2D.Impulse); jump = false; } }
    When jump is true, we add a jump force to the character. jumpForce is a public variable declared outside the methods at the top of the code: public float jumpForce = 10.0f;

How do I change my character from placeholder graphics to different graphics?

  • In a file explorer, Make sure your new files are in your Unity project, in the same place as your placeholder graphics.
  • In Unity, find the files in the Project tab and select all the animation frames. Drag & drop them into your Scene window.
  • *W.I.P.* Is there a better way than renaming each file one by one? This is what I had to do and it was tedious.

How do I make something a prefab again?

  • Select the parent object and drag it on top of the “Prefabs” folder. The text will turn from black to blue.

Unity 4.6 has a new GUI system!

Here is the old way of Adding a User Interface and Heads-Up Desplay (HUD)

  1. Click GameObject > UI > Canvas from menu
  2. Rename your Canvas to HUDCanvas
  3. Add Component > Miscellaneous > Canvas Group
  4. Un-check Interactable and Blocks Raycasts
  5. Right-click HUDCanvas and create a UI > Image
  6. Rename this game object to ScreenFader
  7. In the Rect Transform component, click the Anchor Presets button (screenshot needed) and Alt-Click the Stretch both option (screenshot)
  8. In the Image Component, click the Color block and choose a color shade.

The new “UGUI” guidelines and code works a little differently.

Here is a tutorial on creating a Start button menu for your game.

I basically made a new scene, saved it to my Scenes folder as “MenuScene,” and created my start menu GUI there.

  1. Click GameObject > UI > Image. This creates a Canvas object with an Image as a child automatically.
  2. Set the image sprite to be your background image (for example, a starry sky).
  3. Click GameObject > UI > Button. Customizing the button to your liking is covered in plenty of new tutorials online.
  4. Making a trigger to actually click the Start Game button and run the second scene in the game itself is covered in the link above.

Object Pooling / Procedural Generation

Instead of instantiating something new and destroying it off screen, create a bunch up front by guessing how many you need.

You create two “buckets”: one bucket contains currently used pieces. The other bucket is a list of disabled / unused pieces. I’m using lists because they are more malleable than arrays.

When the scene starts, you either want to have the pieces you’re going to be spawning already in the scene and put in the Disabled list from the Inspector,
or you want to instantiate them from the pool manager / game manager (active chunks).

How many level environment chunks need to exist at once? Three? Create a new Game Object not attached to anything else. Drag your pool manager or game manager script onto this Game Object. Make sure to populate the Level Chunk Prefabs starting number with your prefabricated level environment pieces (by dragging and dropping them into the appropriate field in the inspector).

The Create function will instantiate the prefabs into the scene, disable them, and put them in your disabled bucket so that they are ready to be enabled
when they are called upon. Make sure to set the distance between your level chunks accurately and consistently.

Comments (0)

Skip to main content