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 TUTORIAL | PROJECT | SPACE SHOOTER |7. Shooting Shoots

UNITY TUTORIAL | PROJECT | SPACE SHOOTER |7. Shooting ShootsUNITY TUTORIAL | PROJECT | SPACE SHOOTER |7. Shooting Shoots

2
Creating “Spawn Point” as Child Object of “Player”
3
Instantiate(Object, position, Quaternion)
4
Using GameObjects in Script as Public
5
shooting logic

ezgif.com-video-to-gif.gif

UNITY TUTORIAL | PROJECT | SPACE SHOOTER |2. The Player Game Object

UNITY TUTORIAL | PROJECT | SPACE SHOOTER |2. The Player Game Object

2
Adding Player GameObject
3
Resetting the position of “Player” into (0, 0, 0)
4
Transform.Position set to (0, 0, 0)
56.png
RigidBody Added | Gravity Unactivated
6
Capsule Collider Added
ezgif.com-video-to-gif
Changing Collider Size
8
Changing the direction of Collider into Z-Axis for configuration
9
Changing the Radius and Height of Collider
10
Adding “Mesh Collider” | only one collider should be added
11
“Mesh Renderer” off to see “Mesh Collider”
13
Simplified “Mesh” added to “Mesh Collider”
14
“Particle” added as Child GameObject of  “Player”
ezgif.com-video-to-gif 복사본
“Particle” effect