Step 4
The blocks for the Patrolling enemy script
In this level we'll add some patrolling enemy characters. These enemies will patrol up and down (or you can make them patrol side to side if you'd like). We'll continue to use the if-statements and less than/greater than blocks that we introduced in the previous steps.
Find a game object to use as an enemy character, and place a few of them throughout your level where you'd like them to patrol.
After you've placed a bunch of enemies in your level, right-click on one and select Add script. We want the patrolling enemy to do these things:
Start moving up or down with a y-velocity be immovable have a tag of killer, so the player will crash when it touches it be flipped horizontally (so that the enemy is facing towards the player) Constantly check if it is outside of its patrol zone (a minimum and maximum number), and then change its velocity to go in the other direction Destroy itself when touched by the player After thinking about each of those behaviours that these enemies should have, find these blocks and drag them into the workspace:
When created (from Events) Set velocity y (from Physics) Set immovable true (from Physics) add tag name on myself (from Sensing) set scale x of myself (from Transform) Constantly (from Events) if (from Control) > ( = from Operators) y position of myself (from Transform) number block (from Operators) set velocity y (from Physics) if (from Control) < ( = from Operators) y position of myself (from Transform) number block (from Operators) set velocity y (from Physics) When I am touched, get toucher (from Physics) if (from Control) instance "toucher" (from Variables) myself has tag (from Sensing) destroy myself (from Controls)
Arrange all these blocks in the workspace in the way you think they should work. Go up a bit and read the list of things we want the patrolling enemy script to do, and think about how you can use these blocks to achieve that. Also take a look at how the last few scripts that we've already made, since this one isn't that much different.
If you're unsure, the next step will help you out.