Join Jesse Freeman for an in-depth discussion in this video Aliens that attack, part of Unity 5: 2D Level Design.
- [Instructor] At this point, we have the basic obstacles of our level laid out. If we take a look, we have the player fly into the first room where there's no danger. As they move into the second room, they have to push the block in order to open the door and from the third to fourth room, they have to overcome the wall and not hit the spikes at the top. But the alien we have in our third room really isn't much of a challenge to fly over. What we're going to do is create a new type of alien that only attacks the player when they get near them and we'll have this alien attack up so the player has to fly higher and faster than they normally would want to to get over to the last room, thus pushing them closer into the spikes.
Before we begin, let's move the current alien that we have over into the last room. Next, we'll go into the JetroidLevelDesign folder, open up the Artwork and let's take a look at alien-b. Let's select alien-b-00 sprite and while holding down Shift, go all the way down to alien-b-10. Then let's drag this into the third room. Now we'll need to create a new animation. Let's call this AlienBIdleAnimation and we'll save it into our Animations folder.
Let's take a look at this animation. Here you can see the eyes of the alien simply move back and forth, making it look more like grass than an actual alien. Next, we're going to create the alien attack animation. Let's call this AlienBAttackAnimation and save it in our Animations folder. Now, we'll select alien-b sprites 11 all the way through 21 and drag these into the timeline.
We'll also change the timeline to 12 frames. If we run this animation, you'll see that our alien comes to life and tries to attack the player when they fly over it. One thing that we're going to want to do though is finish off the animation so that the alien can go back to its idle position. In order to save space, since the animations going back down are similar to the ones going up, we're going to do this by hand. Let's start by taking a look at alien-b-16, which should be the height of the attack animation.
As we go backwards, you'll see that the alien goes down. Let's start with alien 14 and drag it into the next frame of animation. We'll also drag 13... 12... and 11. This will get us back down to the beginning idle animation. Let's run this and make sure it looks right. Now that we have our two animations, let's connect them up in the Animator.
Let's select the alien game object we created in the hierarchy and we'll call this AlienB. Also, we'll reactivate the Animator if it was disabled and hit Save in order to save all the changes we made to the animations. Next, we'll connect the AlienBIdleAnimation to the AlienBAttackAnimation and let's also connect the AlienBAttackAnimation back to the idle animation. We'll create a new int parameter called AnimState and we'll leave its default value at zero.
Next, let's select the first transition from idle to attack and remove the transition values. At the bottom, we'll add the new condition for AnimState and we'll set it equal to one. Next, we'll go from the attack back to the idle and do the same thing. Here, we'll set the AnimState equal to zero.
If we change our changes in the scene and run the game, we should be able to test out this animation. As you can see, the alien's eyes will bob back and forth and if we change this to one, it'll attack. Changing it back to zero will go back to the idle animation. Now, let's go ahead and create a class for our new alien. We'll select our JetroidLevelDesign folder, go into the Scripts folder and create a new C# script.
We're going to call this AlienB. Let's open up the script in MonoDevelop. At the top of our script, we're going to create a private field to get a reference to the Animator class and we'll call it animator. Inside of the Start method, we'll set the animator value equal to GetComponent and pass in the Animator class reference. We can remove the Update method. And now let's add a new handler for OnTriggerEnter2D and we'll accept a Collider2D type and we'll call this target.
Next, let's test that the target is actually the player. We'll look at the target's gameObject tag and compare it to the Player string. If this is true, let's reference the animator and call SetInteger. Here we'll pass in AnimState with a value of one to display the attack animation. Let's go ahead and save this and go back into Unity. We can drag the AlienB script onto our new AlienB game object and let's select the AlienB game object and we're going to need to add a Box Collider 2D and set it to trigger.
Now, to make this a little bit easier to test, I'm going to go ahead and move the player closer to the room. Now I plan on moving this back, but for right now as we test it's better to have the player closer to the enemy so we can do this quickly. Let's go ahead and run the game and walk over to the alien. As you can see, when the player enters the alien's space, it automatically attacks and this should normally kill the player. The last thing we want to do is slow down the animation for the attack.
Let's go into the Animator, select our AlienB, and double-click on the AlienBAttackAnimation. First, we want to disable the loop and second, by reselecting it, we're going to change the speed to 2.5. Let's save our changes and rerun it to see what happens. Now we want to take the animation for the attack and speed it up. And now we can pick up from when I did the testing of the play. As you can see, the alien's attack is much quicker now.
So as soon as the player gets into range of the alien, it's going to attack it instantly.
- Importing assets
- Building a level
- Adding collisions and layers
- Populating the level
- Creating and triggering doors
- Creating switches and gizmos
- Adding different enemies
- Building a UI for an air meter timer