UNITY | PROJECT | SPACE SHOOTER 2D | 10. Game Scrolling BackGround

1
“EnemySpawner” Script
  • Add a code to reset “maxSpawnRateInSeconds” to 5
  • Refer to “GameManager” Script

 

2
Star Sprite & Script
3
Creating “Star” Prefab
4
“Star” Script
  • Claim “Speed” variable for the speed of “Star”
  • In “Update()” method
    • Get, Change and Set “transform.position” of “Star”, this script is attached to
    • in “if” statement,
      • if y position of “Star” goes down below the screen, change the position of “Star” on the top with x position random

 

5
Creating “StarGenerator” from EmptyGameObject | Adding “StarGenerator” Script

 

6.1
“StarGenerator” Scrip
  • Claim “MaxStars” variable for the number of Stars
  • Create “Color” Array for “Star”
  • In “Start()” method
    • Get the Scree
    • In “for” Loop,’
      • Instantiate “star”
      • Get Component “SpriteRenderer” to Star, instantiated from the prefab
      • Change “Color ” property “”of “SpriteRenderer”
        • color is changed with the calculation “[i % starColor.Length]”
      • Set the position of “Star” randomly
      • Set the speed with Random.value (generating a value between 0.0 and 1.0)
      • Set the position of Star in the position of “StarGenerator”

 

6.3
Prefab “Star”

 

 

20
“Star”
7
Adding “Sorting Layer”
8
Adding “Sorting Layer”
9
Setting “GamePlay” Layer to the GameObjects
10
Importing “Planet”

 

6.2
Creating “Planet” Prefab | Adding “Planet” Script
14
Creating “PlanetGenerator” from EmptyGameObject | Adding “PlanetController”
15
Setting Planet Prefab for each sprite
16
Setting Planet Prefab for each sprite
17
Setting Planet Prefab for each sprite
18
“Planet” Script
  • Claim “speed” variable for “Planet” speed
  • Claim “moving” as “Flag”
  • Claim the Screen as local variable
  • In “Awake()”
    • Set the Flag to false
    • Set the screen
  • In “Update()”
    • if “Planet” is not moving,
    • Change the position of Planet until the bottom of the screen
      • change the flag to false
  • In “ResetPostion()” method
    • Set the position of “Planet”  in the top of the screen with the x position random
19
“PlanetController” Script
  • Claim an Array of Planets as public
  • Create a Queue to hold “Planet”
  • In “Start()” method
    • Enqueue the planets in the queue
    • Invoke “MovingPlanetDown” every 20seconds
  • Create a Function, “EnqueuePlanets()”,
    • Create a “foreach” loop
      • if a planet’s position is below the screen and planet is not moving
        • Reset the Planet’s position by using “ResetPosition()” method of “Planet” class
        • Enqueue the disappearing  planet in the Queue
  • Create a Function, “MovePlanetDown”
    • Put “EnqueuePlanet()” method to collect Planets
    • Get a Planet from the queue by using “Dequeue()”
      • Get Component of “Planet” to set the flag to true
21
“Planet” Scrolling