Week 6: Throwing a projectile
Today we’ll create an object that we can throw in our game. We’ll use conditional logic to determine where to place the projectile and determine what direction it needs to travel.
Programming concepts we’ll learn:
- If-else control structures
1: Setting up the projectile
In this step we’ll get the projectile ready to throw. Watch the video below to see how this projectile needs to be set up.
- Place a throwable object in the playable level to be the projectile
- Right-click add script
- “When created” from events
- “Set physics enabled true” from physics, change to false
- “Set visibility true” from looks, change to false
- “Add tag” from sensing, set tag to “bullet”
- Close and save script
- Edit script on player
- “When created” from events
- “Add tag” from sensing, put it in when created
- Change tag to “player”
- Close player script and save
- In the next step we’ll make a keyboard key trigger the throwable object
2: Throwing the projectile
In this step we’ll use the spacebar key to make the projectile get thrown. Watch the video below for a refresher on using if-else statements.
- Edit the projectile script
- “When backspace/delete pressed” from events
- Change to “spacebar”
- Duplicate “set physics enabled” and “set visibility” and put in “when spacebar pressed”, set them both to true
- “If do” from control flow
- Click on settings icon to change to “if else”
- “=” from operators
- “Scale x of myself” from transform, put in left side of =
- Replace “myself” with “first instance by tag” from sensing
- Change tag to “player”
- “0” from operators, put in right side of =
- Change 0 to 1
- “Set velocity x” from physics, put under “if”, set to 500
- Duplicate “Set velocity x 500”, put inside “else”
- Change to -500
- Save and close script
- Edit player script, add player tag When Created
- Play to test
- The projectile should become visible and fly off when spacebar is pressed
- In the next step we will position the projectile in front of the player
3: Positioning the projectile
In this step we’ll position the projectile in front of the player. Watch the video below for a refresher on positioning objects.
- Edit the projectile script
- “Set x position of myself to 0” from transform
- “+” from operators
- “X position of myself” from transform
- Replace “myself” with “first instance by tag” from sensing
- Change tag to “player”
- “0” from operators, put in right side of +
- Change 0 to 100
- Duplicate all these to set y pos
- Change to + 50
- Duplicate the x and y blocks for inside “else”
- Change x position to - 50
- Close, save the script and play the game
- Should be able to throw around projectiles
- In the next step we’ll get the projectile to detect collisions
4: Colliding with and destroying obstacles
In this step we’ll get the projectile to detect when it collides with other objects. Watch the video below for a refresher on collision detection.
- Edit the projectile script
- “When touched get toucher” from physics
- Duplicate “set physics enable” and “set visibility”, put both in “when toucher get toucher”, set both to false
- “If” from control flow, drag under “set visibility”
- “Myself has tag” from sensing
- Replace “myself” with “instance toucher” from variables
- Change tag to “obstacle”
- “Destroy myself” from control flow
- Replace “myself” with “instance toucher” from variables
- Close script and save game
- Play game and throw at things
- Next week we’ll learn how to create speed boosts, teleporters, and double-jumping