- Cat Sprite can move to the left and right when the right and left arrow keys are pressed.
- Cat sprite can shoot a bullet which is a Ball sprite by pressing the space button.
- Ball sprite move up starting from the cat sprite position and disappear at the top edge of the screen.
In Sratch, we need following two Sprites: the Cat Sprite (Sprite1) and the Ball Sprite (Ball).
Script for the Cat Sprite:
Script for the Ball Sprite:
In Unity, the above functionality will be implemented in two steps.
- Step 1: Shooting without the ball disappear in certain position.
- Step 2: Shooting with the ball disappear in certain position.
Step 1: Shooting and Ball doesn't Disappear
Script for the Ball Sprite:
To be more detailed, below is the comparison between script in Scratch and in Unity for things that are related with the above script.
Create Sprite Clone
In Scratch:
In the above script the create clone block is used for creating the clone of the Ball Sprite. When the Ball sprite duplicate is just created we set its position to the Cat Sprite position. This way, the Ball Sprite will move starting from the Cat Sprite position.
In Unity:
Before creating the clone of Ball Sprite, we shall first create a variable with type Transform like the following:
The BulletPrefab variable above shall later be connected with the prefab of Ball Sprite. Prefab is a Sprite that has been put into the asset so that its duplicate can be created.
Below video is a tutorial on how to create a prefab from the Ball Sprite and connect it with the BulletPrefab variable in the Cat Sprite script.
Next, for creating the duplicate of the Ball Sprite prefab, we use the following Instantiate command.
There are three parameters for the Instantiate command:
- Parameter 1 is the prefab that will be cloned and then shown on the screen. This parameter is filled in with the BulletPrefab that is connected with the Ball Sprite. This way, the Ball Sprite will be cloned and shown on the screen.
- Parameter 2 is the position where the prefab will be shown. This parameter is filled in with the position of the Cat Sprite which is the transform.position.
- Parameter 3 is the initial rotation of the prefab. This parameter is filled in with Quaternion.identity which means no rotation (0 rotation).
Step 2: Shooting and Ball Disappear
In Step 1, after shooting the ball never disappear. This way the number of balls in the game keep increasing and at the end the game will get slow. Bellow is what will appear when the number of Bullet Clone keep increasing.
We see above that after shooting the Bullet Clone which is the duplicate of Ball Sprite is never deleted. To delete it we can just update the Ball Sprite script to the following script.
In the above script, if the Y position of the Ball Sprite is more than 5.0 (top of the screen) then we tell computer to delete (Destroy) the Ball Sprite.
Bellow is a more detailed comparison between the script in Scratch and in Unity related to the above script.
Is Position Y > Certain Value ?
In Scratch:
In Unity:
Delete this Sprite Duplicate (Clone)
In Scratch:
In Unity:
Tidak ada komentar:
Posting Komentar