From Test-Scratch-Wiki
This tutorial explains how to create the effect of shooting, launching, or throwing an object. The code in this article does not simulate velocity or parabolic motion, but to a projectile that, after the initial force, is not acted upon by any traditional force (gravity, wind, etc.), though it may be obstructed by walls or targets.
In each example, the projectile shoots when the space key is pressed, but the trigger can be any Boolean Block.
Two sprites, no cloning
These scripts require two sprites; a player sprite and a projectile sprite. No cloning is used.
Semi-Automatic
This script requires the player to release the trigger button before firing again. This reduces the rate of fire:
when gf clicked // in the player's character sprite forever wait until <key [space v] pressed?> broadcast [shoot v] and wait wait until <not <key [space v] pressed?>> // the player can only shoot once at a time end when I receive [shoot v] //in the projectile sprite go to [You v] //start at the player show repeat until <<touching [intended target(s) v]?> or <touching [edge v]?>> //moves the sprite until it is touching something that will make it go away move (10) steps end hide
Automatic
This script allows the player to simply hold the trigger button for continuous fire:
when gf clicked //in player's sprite forever wait until <[space v] key pressed?> broadcast [shoot v] and wait wait (0.1) secs // does not wait for the trigger to be released; change to amount of seconds between each shot end when I receive [shoot v] //in projectile sprite go to [You v] show repeat until <<touching [intended target(s) v]?> or <touching [edge v]?>> move (10) steps end hide
Two sprites, one cloning the other
This method is similar to the one above, only it incorporates cloning to allow for more than one bullet on the screen at once. Once again, both a player sprite and a projectile sprite are needed.
Semi-Automatic
when gf clicked // in player sprite forever wait until <[space v] key pressed?> create clone of [bullet v] // clones the projectile sprite wait until <not <[space v] key pressed?>> end when I start as a clone // in projectile sprite repeat until <<touching [intended target(s) v]?> or <touching [edge v]?>> move (10) steps end delete this clone
Automatic
when gf clicked // in player sprite forever wait until <[space v] key pressed?> create clone of [bullet v] wait (0.1) secs // change to amount of seconds between each shot end when I start as a clone // in projectile sprite repeat until <<touching [intended target(s) v]?> or <touching [edge v]?>> move (10) steps end delete this clone
One sprite cloning itself
This method is more efficient because it only requires one sprite with two costumes: the player and the projectile.
Semi-Automatic
when gf clicked switch costume to [player v] forever wait until <[space v] key pressed?> create clone of [myself v] wait until <not <[space v] key pressed?>> end when I start as a clone switch costume to [bullet v] // makes the clone look like a projectile repeat until <<touching [intended target(s) v]?> or <touching [edge v]?> move (10) steps end delete this clone
Automatic
when gf clicked switch costume to [player v] // makes sure the parent sprite looks like the player forever wait until <[space v] key pressed?> create clone of [myself v] wait (0.1) secs // change to amount of seconds between each shot end when I start as a clone switch costume to [bullet v] // makes the clone look like a projectile repeat until <<touching [intended target(s) v]?> or <touching [edge v]?> move (10) steps end delete this clone
Tips
- To make the projectile aim at your mouse pointer, place the following block after the "go to x: () y: ()" block:
point towards [mouse-pointer v]
- If the length of the sprite is less than 10 pixels, replace both 10's with at most the length of the sprite, or the sprite may pass through its intended targets.
- You can also change the length of the sprite's costume so that it will still detect the intended targets.