Selasa, 28 Februari 2017

Enemy To Be Shot At

This time we are going to create enemies that can be shot by the Cat Sprite. The enemies shall be moving from left edge of the screen to the right and disappear on the right edge of the screen. If the enemy touch the Ball Sprite then it disappear. For that, we need two new sprites.



Enemy sprite is the one that will be shot at and the Game Manager sprite is responsible to create the Enemy sprite duplicate every 1 second interval.


Script for Game Manager:


Script for Enemy Sprite:



The same functionality can be done in Unity using the following script.

Script for Game Manager:


Script for Enemy Sprite:


Beside the above Unity Script we still need to do the following actions:
  • Create the Game Manager object using the Create Empty menu. 
  • Create the Enemy Prefab that will be connected to the EnemyPrefab variable in the Game Manager script. 
  • Add the Rigidbody 2D component to the Ball Sprite and the Enemy Sprite and set the gravitation parameter with 0 value so that the sprites will not fall down because of gravity. This component is responsible to give the mass (weight) effect to the Ball and Enemy Sprites that is needed for collision detection between those sprites. 
  • Add the Box Collider 2D component to the Ball and Enemy Sprites. This component is responsible to give boundary that will be used for detecting the collision between sprites.    

Below is tutorial video for the above actions.



To be more detailed, below is the comparison between Unity and Scratch of things that are related with the above script.

Repeat Forever (Forever Loop)

In Scratch


In Unity


Random Number

In Scratch


In Unity


Check Collision

In Scratch



In Unity


OnCollisionEnter2D is a function that is executed in Unity if the Sprite touch other sprite. The collision parameter contain the other sprite that is hit by this sprite. In this function we can check the other sprite name to know what kind of sprite is being hit.


Assignment - 1

Create a Unity program in which there is new enemy that appear from the top edge of the screen every 0.5 until 1 seconds, moving down, and disappearing when touching the bottom edge of the screen. If this new enemy touch the Ball Sprite then the enemy also disappear. Below is the video of the working program.




Assignment - 2

Update the program in Assignment-1 so that the Cat Sprite shall disappear if it hits Enemy that is moving down or moving right.

Clue: When we add the Rigidbody 2D and Box Collider 2D components to the Cat Sprite, unwanted things happens. During shooting, there will be collision between Cat Sprite and the Ball Sprite (Bullet) so that the Cat Sprite will be displaced and we do not want that. To solve the problem, when shooting, the initial position of Ball Sprite has to be moved to a position just above the Cat Sprite position so that collision will not occur. To do that, see bellow picture as a clue:



Assignment - 3

Update the program in assignment-2 so that enemy that move to the right can shoot a bullet. This enemy bullet will be moving down and it is shot every 1 until 2 seconds. If the enemy bullet touches the bottom of the screen it disappears. If the Cat Sprite touches this enemy bullet, the Cat Sprite shall also disappear.













Tidak ada komentar:

Posting Komentar