Week 5: Scripting a moving obstacle


Today we’ll program an obstacle that patrols back and forth from one place to another. We’ll save the initial position of the object as a variable, start it moving in a direction, and then check how far it has moved from its original position.

Programming concepts we’ll learn:

  • Comparative operators
  • If-else selection control structure
  • Variables

1: Placing the moving obstacle

In this step we’ll place the obstacle and set it up to program it. Watch the video below for a refresher with placing objects and creating variables.

  • Place an object in the playable level to be the patroller (make sure it has enough room to move around)

  • Right-click add script
  • “When created” from events
  • “Add tag on myself” from sensing
  • Change tag to “obstacle”
  • “Set true/false i to” from variables
  • Create new variable named “starting x position”
  • Change to number
  • “X position of myself” from transform
  • “Set x velocity to” from physics
  • Set to 200

  • In the next step we’ll program the object to check its position and change its velocity

 

2: Comparing positions

In this step we’ll make the moving object compare its position to its starting position. Watch the video below to learn how to use comparative operators to compare numbers.

  • “Constantly” from events
  • “If do” from control flow
  • =” from operators
  • Change to > (greater than)
  • “X position of myself” from transform, put in left of >
  • +” from operators, put in right of >
  • “Starting x position” from variables, put in left of +
  • “0” from operators, put in right of +, change to 300 (or however far you want the object to move)
  • “Set x velocity to 0” from physics, change to -200

  • Save script and play game
  • In the next step we’ll add another check to bring the object back once it has turned around

 

3: Completing the loop

In this step we’ll make the object turn back around so that it remains in a loop moving back and forth. Watch the video below to learn how to modify an if-statement to become an if-else-if statement for programming more complex decision making.

  • Click on the settings icon on the “if do” block
  • Drag else-if into the if block

  • Click icon again to close popup
  • Duplicate “x position of myself > starting x position + 300”
  • Drag into else-if
  • Change to “x position of myself < starting x position”
  • Duplicate “set velocity x -200”, drag into else-if, change to 200.

  • Play game to preview
  • In the next step we’ll flip the player to face the correct direction

 

4: Flipping the Player’s direction

In this step we’ll make the player character flip to face the correct direction. Watch the video below to learn how to change an object’s scale and center its anchor point.

  • “Set scale x of myself to 0” from transform
  • Put this block in “when left arrow pressed”
  • Change 0 to -1
  • Duplicate this block in “when right arrow pressed”
  • Change -1 to 1.

  • Center anchor point

  • Next week we’ll set up a projectile the player can throw

 

results matching ""

    No results matching ""