UNITY | PROJECT | SPACE SHOOTER 2D | 3. Player Bullet and Player Shooting

SPACE SHOOTER 2D

3. Player Bullet and Player Shooting

3
Bullet Sprites
  • Importing “Bullets” sprite

 

5
Sprite Editor | Slice

 

  • by using “Sprite Editor”, slice “Bullets” sprites into 2 sprites
    • when slicing, set the type into “Grid By Cell Size”
    • when slicing, set the Pixel Size as 24×24

 

4

  • Having 2 bullet sprites

 

6
Sprite Setting
  • for the bullet sprite in the inspector
    • deselect “Generate Physics Shape”
    • Filter Mode into “Point(no filter)”
    • Max Size into “1024”

 

7
Adding prefab in the scene
  • Add “Player Bullet” sprite in the scene to configure like adding a script

 

8
Adding a script to Bullet GameObject
  • Adding “PlayerBullet” script to “PlayerBullet” GameObject

 

9
“PlayerBullet” Script
  • set the public variable “speed” for the speed of the bullet
  • In “Update()” method
    • get the bullet’s current position by using “transform.position”
    • computing the bullet’s new position = the change of position when we shoot the bullet
      • only need to change the “y” value
    • to smooth the shooting, use “Time.deltaTime”
    • Don’t forget to update the position into “transform.position”
  • for the memory usage, destroy the bullets going outside the screen
    • when setting the screen range, only consider “y” value or “height”
    • “Camera.main.ViewPortToWorlPoint()”

 

10
Setting the speed in the inspector | Public Variable
ezgif.com-video-to-gif
Shoot a bullet

 

11
Making a Prefab
  • Make the Bullet Prefab by dragging the bullet we configured in the Hierarchy window
    • this is the Bullet we will continuously instantiate

 

12
Empty GameObject

 

13
GameObject BulletPosition
14
GameObject BulletPosition
  • Make 2 Empty GameObject to Create “BulletPosition”, where Bullets come out
    • Set the position of  “BulletPosition” on “Player” GameObject

 

15
Child Object
  • Make “BulletPosition” as a ChildObject of “Player”
    • “BulletPosition” belongs to “Player”

 

16
“Player Control
  • Set Public Variables to use those components in the script

 

17
Public Variable | Reference

 

18
Player Control
  • In “Update()” method
    • Instantiate Bullet Prefab when “SpcaeBar” is pressed
    • set the initial position of a bullet after instantiating

 

ezgif.com-video-to-gif-2
Player Shooting | Destroying Bullets

 

UNITY | PROJECT | SPACE SHOOTER 2D | 2. Player Input Movement

SPACE SHOOTER 2D

2. Player Input Movement

2
Player Control Script
  • Creating & Adding “PlayerControl” script

3
Player Control Script | Update() | Input.GetAxisRaw()
  • setting “speed” variable as public to see in the inspector
  • In “Update()”
    • set horizontal & vertical input by using Input.GetAxisRaw()
      • with Input.GetAxisRaw(), the values will be -1, 0 or 1
    • with the two inputs, make “direction”
      • by adding “.normalize”, make a unit vector
    • add a customized fuction “Move()” to move “Player”
      • we need this fuction in “Update()” because “Player”‘s position must be changed and detected every frame

 

4
Player Control script | Move() | Vector2 | Camera.main.ViewportToWorldPoint()

 

5
Player Control script | Move() | transform.position | Mathf.Clamp()
  • get the Player’s current position with “transform.position”
    • transform.position of the gameObject this script is attached to
  • calculate the new position in “currentPosition” variable
    • with “speed” variable, we can change the speed of “Player”
      • “direction” is the normalized vector2
      • “Time.deltaTime” is very important with this, we smooth the movement
2. Player Input Movement
without “Time.deltaTime”, the value(“direction”) is almost (0, 0), (1, 0)……

 

6
Setting Player’s Speed | Public Variable

 

1
Player Input Movement
2
Player Input Movement | without Time.deltatime
  • without “Time.deltaTime”, “Player” moves too dynamically