This article has links to websites or programs outside of Scratch and Wikipedia. Remember to stay safe while using the internet, as we cannot guarantee the safety of other websites. |
This tutorial explains how to create the effect of shooting, launching, or throwing an object. The first few scripts in this article do 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.
The projectile shoots when the space key is pressed or the mouse is clicked, 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?> or <mouse down>> broadcast (shoot v) and wait wait until <not <<key (space v) pressed?> or <mouse down>>> // the player can only shoot once at a time end when I receive [shoot v] //in the projectile sprite go to (The player 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 <<key (space v) pressed?> or <mouse down>> 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 (The player 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 <<key (space v) pressed?> or <mouse down>> create clone of (bullet v) // clones the projectile sprite wait until <not <<key (space v) pressed?> or <mouse down>>> 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 <<key (space v) pressed?> or <mouse down>> 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 <<key (space v) pressed?> or <mouse down>> create clone of (myself v) wait until <not <<key (space v) pressed?> or <mouse down>>> 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 <<key (space v) pressed?> or <mouse down>> 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 the 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.
- One can also change the length of the sprite's costume so that it will still detect the intended targets.
Shooting Projectiles with Trajectories
In Scratch, the Trajectory formula is used for projects such as:
- Rocket or baseball simulators
- Tank games
- Anything that includes the curve of a projectile
The formula(e) calculate(s) where on the screen the projectile should be. As well as calculating this, there are many other formulae that calculate different things
These are the formulae:
Horizontal velocity: v°x = v°cos(ß) Horizontal distance: x = a+v°xt Vertical velocity: v°y = v°sin(ß)-gt Vertical distance: y = b+v°sin(ß)-0.5gt²
v° is the initial velocity, ß is the projectile angle (from -90 to 90), a & b are the starting coordinates, g is the informal gravity constant 9.81, and t is the time since the launch.
Other Formulae
Maximum height: H = b+[v°²sin²(ß)]/[2g] Horizontal range: R = [v°²sin2(ß)]/[g]-a
sin(ß) and cos(ß) are the vertical and horizontal components (or velocities).