Below is the script in Scratch for shooting in which the shooting can only be done after waiting 0.5 seconds since the last shooting.
The above script uses a variable called CanShoot. The shooting can only be done if the CanShoot variable has the value 1. After the shooting is done, the CanShoot variable is immediately set with value 0 so that shooting can not be done anymore. The shooting can only be done again after the CanShoot variable is set to value 1 after waiting for 0.5 seconds since the last shooting.
Note that in the above script we use broadcast StartCoroutine command to call the WaitForCanShoot block. This is done so that the program for moving the Cat Sprite to the left and right can be executed together with the program for waiting the shooting for 0.5 seconds. In this way, the Cat Sprite can still move while the sprite is waiting for 0.5 for the next shooting.
The same thing can be done in Unity with the following Cat Sprite.
Below is the detailed comparison between script in Scratch and in Unity.
Creating Variable
In Scratch:
In Unity:
It means create a variable called CanShoot with Integer type. Integer is whole numbers like -3,-2,-1,0,1,2,3.
Setting Variable Value
In Scratch:
In Unity:
Comparing Variable Value
In Scratch:
In Unity:
Note that in Unity, for comparing we use two symbols of =.
Parallel Process
In Scratch:
In the above script the broadcast StartCoroutine command does not wait until the WaitForCanShoot block is completed. This way, the next command after the broadcast Coroutine can be immediately executed together with the execution of the WaitForCanShoot block.
In Unity:
Waiting
In Scratch:
In Unity:
In Unity the above wait command can only be done inside a function that is called using the StartCoroutine.
Tidak ada komentar:
Posting Komentar