Skip to content

Instantly share code, notes, and snippets.

@jahe
Last active June 26, 2017 21:05
Show Gist options
  • Select an option

  • Save jahe/389d2f746ab7caf1192365b48e4597b1 to your computer and use it in GitHub Desktop.

Select an option

Save jahe/389d2f746ab7caf1192365b48e4597b1 to your computer and use it in GitHub Desktop.
Unity 2D Notes

Hierarchy

  • backgrounds - A GameObject without a Sprite. It has a Script BackgroundParallax that gets env_bg, env_BigBen, etc. as a list of parallaxable GameObjects.
    • Clouds
    • env_Bank
    • env_BigBen
    • Fog
    • RiverMid
    • RiverTop
  • foregrounds - A GameObject that has only a Transform Component
    • Transform
      • Position Z: 0
    • Children
      • env_PlatformUfo - has a Polygon Collider 2D
        • Tag: ground
        • Layer: Ground
        • Transform
          • Position Z: 0
        • Sprite Renderer
          • Material: Sprites-Default
          • Sorting Layer: Foreground
          • Order in Layer: 1
        • Polygon Collider 2D
          • Material: None (Physics Material 2D)
      • env_TowerFull - has a specific Material on its Box Collider 2D component
        • Tag: Obstacle
        • Layer: Default
        • Transform
          • Position Z: 0
        • Sprite Renderer
          • Sorting Layer: Foreground
          • Order In Layer: 1
        • Box Collider 2D
          • Material: PlatformEnd
      • env_UfoLegs - has no collider due to the fact that the character should be able to walk through them
        • Transform
          • Position Z: 0
        • Sprite Renderer
          • Sorting Layer: Foreground
          • Order in Layer: 8
  • hero - An empty GameObject that represents the Hero with multiple child GameObjects
    • Tag: Player
    • Layer: Player
    • Transform
      • Position Z: -1
    • Rigidbody 2D
    • Box Collider 2D
    • Circle Collider 2D
    • Animator
      • Controller: Character - it is not driving a 3D biped
      • Apply Root Motion: No - because it is not 3D biped
      • Animate Physics: Yes - because it is not 3D biped
      • Culling Mode: Based on Renderers
    • Audio Source
    • Player Control (Script)
    • Player Health (Script)
    • Lay Bombs (Script)
    • Children
      • Bazooka
        • Layer: Player
        • Transform
          • Position Z: 1 - is used to sort the rendering depth of each heros piece
        • Sprite Renderer
          • Sorting Layer: Character
          • Order in Layer: 0
      • body
      • groundCheck
      • Gun - An empty GameObject that is positioned at the bazookas front so that this position can be used as a spawn point for the rockets fireing out of it (transform.position)
        • Gun (Script)
          • Rocket: rocket (Rigidbody 2D) - Prefab of the rocket
          • Speed: 25 - Speed of the rocket
        • Audio Source
          • Audio Clip: bazooka

All those elements in backgrounds are assigned to one Sorting Layer: Background. Each element then has a specific Order in Layer value (e.g. 8)

Each element in foregrounds has a Box Collider 2D in their Components to let the character walk upon them.

Spritesheet based Animation

  • Design each Frame in Photoshop and let Unity intepolate the frames between them

Animating each characters sprite over time in "Animation > Dope Sheet" (Keyframing)

  1. Select the Sprite you want to animate
  2. Move the Timeline to the right
  3. Move the Sprite to the target position

Animation Representations (Tabs on the bottom left of the Animation window)

  • Dope Sheet
  • Curves
  • Switch between them to easily adjust timing and design

With these Animations it is possible to design a state machine for our hero character. When a state transition is triggered via code the animations can be switched out. Therefore an Animator Component is needed on the hero GameObject.

Animator Tab

  • Provides a graphical FSM diagram
  • In order to drive our animations in time with the physics engine
  • You can define parameters (e.g. Speed=0.0, Jump=false, Shoot=false, Die=false)

For Shooting he added a new layer "shooting" in the Animator

  • Weight: 1 - Override motion of the base layer
  • Any State -> Shoot -> Empty State
  • State "Shoot"
    • Speed: 1
    • Motion: Shoot
    • Transitions
      • shooting.Shoot -> shooting.Empty State
  • Transition: Any State -> Shoot
    • Atomic: No
    • Preview source state
    • Conditions
      • Shoot - The Animator parameter that can be triggered from code

PlayerControl.cs

  • Moves the hero via 2D physics forces

rocket

  • flame
    • Sprite Renderer
      • Sprite: flame2 and flame1 - switched within the animation (Dope Sheet: flame:Sprite, flame:Position)
      • Sorting Layer: Character
      • Order in Layer: 0
    • Animator
      • Controller: Flame
      • Apply Root Motion: false
      • Animate Physics: true
      • Culling Mode: Based on Renderers
  • rocket
  • trail - Smoke emitted by the rocket
    • Particle System
      • Duration: 5.00
      • Looping: Yes
      • Play On Awake: Yes
      • Max Particles: 1000
      • Emission: Yes
      • Shape: No
      • Velocity over Lifetime: No
      • Limit Velocity over Lifetime: Yes
      • Force over Lifetime: No
      • Color over Lifetime: Yes
      • ...
      • Texture Sheet Animation - Assign a Sprite with the different states of the animation
        • Animation: Single Row
        • Frame over Time: 1-4
        • Cycles: 1
    • Set Particle Sorting Layer (Script)
      • Sorting Layer Name: Foreground
      • Smoke Shader: Particles/Additive
      • Particle Texture: The Sprite with the 4 clouds for the animation of the smoke

rocketExplosion

  • Sprite Renderer
    • Sprite: part_explosion_1 to part_explosion_4 (switched by the animator)
    • Material: HalfAlpha
    • Sorting Layer: Foreground
    • Order in Layer: 0
  • Animator
    • Controller: RocketExplosion
    • Apply Root Motion: Yes
    • Animate Physics: No
    • Culling Mode: Based On Renderers
  • Audio Source
    • Audio Clip: rocketExplode
    • Play On Awake: Yes

Tags (Edit > Project Settings > Tags and Layers)

  • ground
  • Crate
  • Enemy
  • Wall
  • Obstacle
  • Bullet
  • BombPickup
  • ExplosionFX
  • HealthBar

Sorting Layers (Edit > Project Settings > Tags and Layers) 0. Default

  1. Default
  2. Background - for parallax scrolling. Locked so that its elements aren't clickable/draggable in the Editor.
  3. Character
  4. Foreground
  5. UI

Assets

  • Scripts
    • BackgroundParallax
  • Sprites
    • _Environment
      • env_Bank
      • env_BigBen
      • ...
  • Prefabs
    • Props
      • rocket
      • rocketExplosion

// Unity's initialisation functions: Start vs Awake TODO

Components

Sprite Renderer

  • Sprite
  • Color
  • Material
  • Sorting Layer
  • Order in Layer

// Bin jetzt bei 14:11: https://www.youtube.com/watch?v=4qE8cuHI93c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment