UNITY | PROJECT | SPACE SHOOTER 2D | 12. Accelerometer​ Input & Shoot Virtual Button

1.1
Changing Build Target to IOS
1.2
Changing Resolution
1.3
Connecting “Unity Remote5” on iPhone

 

2.1
“Player Control” Script
  • Set the input sources for x&y from iPhone’s Accelerometer
  • Create a Vector2 from the 2 x, y inputs
  • Limit the vector(direction) into 1 by using “Vector2.sqrMagnitude”
    • Think about the Lenth of Vector
    • Relate this with the Pythagorean theorem

 

2.2
Inputs from Accelerometer working but…..
2.3
“PlayerControl” Script
  • Subtract the initial value of Y input from the whole Y input to prevent the player ship from going down when grabbing the phone downwards
2.4.1
Increasing the speed
2.4
Inputs from Accelerometer working
3.1
Importing Shooting Button
  • Create “UI Button”
  • Add “Shoot Button” image to “Source Image” of “UI Button”
  • Set the anchor to “Right-Bottom”
3.2
“Player Control” Script
  • Create a Function, “Shoot()” to make the button work
    • move the code for shooting in “Update()” to “Shoot()”
4
UI Button | On Click
  • Set the action when the button is clicked
    • Add “Player” to “On Click()” and Set “Shoot()” function
5.1
“On Click()”
12. Accelerometer Input - Shoot Virtual Button
“GameManager” Script
  • Get the reference for ShootButton
  • When Case “GamePlay”
    • Set “shootButton” active
  • When Case “GameOver”
    • Set “shootButton” disactive
6.2
“ShootButton” Disactive in the Inspector
6.3
“Shoot Button” Working

UNITY | PROJECT | SPACE SHOOTER 2D | 8. Lives UI, Play Button, & Game Manager

2
Importing UI Sprites
3
Adding UI Image
  • Adding UI Image for Score & Lives

 

4

  • Adding Sprite to UI Image as “Source Image”

 

5
UI Image Applied

 

6
UI Anchor

 

7.1
Adding & Configuring Sprite to UI Image for Scores UI
  • Adding Sprite to UI Image as “Source Image”

 

7.2
UI Anchor

 

8
Creating UI Text
  • Creating UI Text to count Player’s Life

 

9
Adding & Configuring UI Text to Image UI
  • In UI Text
    • set the default Text as “0”
    • change Font Style & Size
108-e1531887916889.png
UI Anchor

 

11
Creating UI Button
  • Create UI Button for Play Button

 

12

13

14
Adding Sprite to UI Button
  • In UI Button
    • Set the size
    • Add Sprites for “Target Graphic”(usual) & “Pressed Sprite”(pressed)

 

15
UI Anchor

 

16
Creating “GameManager” Script
17
Adding Reference
181.png
“GameManager” Script
  • To control PlayButton&Player with GameManager
    • create Public Object as a Reference
    • create an Enumeration, “GameManagerState” to tell Game State
      • Opening, GamePlay, and GameOver
    • create a Function, “UpdateGameManagerState()”
      • use “Switch Statement”
        • make 3 cases

 

19.1
“Play Button”
  • now, Play Button reacts to Clicking but nothing happens

 

19.2
“PlayerControl” Script
스크린샷 2018-07-18 오전 2.21.17
“Player”
20.1
“PlayerControl” Script
ec8aa4ed81aceba6b0ec83b7-2018-07-18-ec98a4eca084-1-22-31.png
“PlayerControl” Script
  • create “GameManager” GameObject as a reference
  • set “Player” inactive for the first moment of Game
  • In “Init()” method, which is called when Game Starts
    • set the current life(“lives”) as MaxLife
    • update UIText by using Text.text property
    • change the number(int “lives”) to string by using int.Tostring() method
    • set Player active when Game starts
  • In “OnTriggerEnter2D()” method(when a Collision with Player happens,)
    • the life is subtracted
    • when the life is 0,
      • use “SetManagerState” in “GameManager” Class with “GameOver”()*(putting the name of “enum”)
      • hide Player GameObject by set it “false”

 

8. Live UI, Play Button, - GameManager
“GameManager” Script
  • update “GameManager” Script
    • in “Start()” method
      • set “GMState” in “Opening”
    • in “UpdateGameManagerState()”
      • in case of “GamePlay”,
        • Set “PlayButton” active to hide it when Game Starts
        • Activate “Init()” method
    • Create” SetGameMangerState()” method to change the Game’s State, having “GameManagerState”(enum) as Parameter
    • in “StartGamePlay()” method
      • change “GMState” into “GamePlay”
      • Don’t forget to put “UpdateGameManagerState()”

 

 

21
“PlayButton” | Sprite & “On Click()”

 

  • In Play Button,
    • Add Sprite for “Target Graphics”(Usual) and “Pressed Sprite”(Pressed)
    • Add “On Click()” event and designate it “StartGamePlay()” method in “GameManager” Class
    • with this part, No Need to write to Star Game

 

231-e1531888420395.png
Adding Reference
ezgif.com-video-to-gif
“PlayButton” & “PlayerLife” working
26
“GameOver” Image added
  • Add “GameOver” Sprite
    • add the sprite to GameManager
    • add the Sprite to the scene
27
GameOver “Off”
  • Turn off “GameOver” for it to be hidden in the first moment of Game

 

28
“EnemySpawner” Script
  • Create a Function, “UnscheduleEnemySpawner()”, to stop the spawning when the game is over” by using “CancelInvoke()”

 

29
“GameManager” Script
  • Create “ChangeToOpeningState()” method
    • use “SetGameMangerState()” method
  • In Case of “GameOver”,
    • put “UnscheludeEnemySpawner()” method to stop the spanwing
  • In Case of “Opening”
    • turn off “GameOver” by using “GameObject.Setactive()”
30
Adding Reference
ezgif.com-video-to-gif
Game Working