您需要 登录 才可以下载或查看,没有账号?注册
x
Okay, so I am aware that we are now six chapters into the book and at this point we still haven’t actually
touched an Android phone! That’s the irony of coding with a desktop IDE, but don’t worry if you’re feeling
impatient—in the very next chapter you’ll get to try deploying your game on an actual Android phone or
tablet. For now, though, we’re going to add a few more touches that will give us the foundations for our
future game development—touches like animations, particle effects, and a heads up display (HUD). When
we get to Chapter 7, it will make sense why we did this first.
And don’t fret. There’s a lot to be excited about right now too: adding simple things like animations is
what will allow you to really add character and charm to your game and elevate it to a point where it starts to
look really rather professional.
Handling Death and Using Particles
Everyone has their own way of handling their mortality. Right now, Squarey’s way is not particularly elegant.
Not only does he just kind of “appear” back at the start of the game instantly, but he also doesn’t give us any
brief pause while that happens. The whole thing is a little too brief to let it really sink in.
It would be nice if we had some kind of death animation, or better yet, a gory explosion.
To do that, we’re going to use the particle system, an effect that lets you scatter pixels all around the
screen and get them to behave in different ways. We’re going to make a particle effect that will look like an
explosion of blood—but you could just as easily use this to make regular explosions, fireworks, fountains,
electricity, and more.
To create your first particle system, choose Game Object ➤ Particle System. You should see a fountain of
slow moving white blobs appear in your scene with options on the righthand side in the Inspector (Figure 6-1).
100
You need to tweak some of the settings here in order to make the effect look a little more like blood. We
can start by changing the Start Color to red (click the white bar) and Start Size to 0.2. Then click the Shape
option below that and choose Sphere instead of Cone. Under Emission, change Rate over Time to read 300.
Now expand the Renderer menu option and click the circle next to the Material option. Choose
Sprites – Default, and the red blobs should change into red squares.
Next, expand Size over Lifetime (note that you need to tick the bullet box first) and drag the righthand
side so that the line is a downward slope. This means the particles will get smaller as they travel through the
air, allowing them to disappear in a way that looks natural rather than suddenly blinking out of existence.
Similarly, you can also do the same for Color over Lifetime. I’ve made my blood get slightly darker as it
expands outward, just to keep things interesting.
I also applied .2 gravity (so that the particles fall downward). Feel free to play around with the other
options as you want—they’re generally pretty self-explanatory.
One thing you need to do, though, is to untick the Looping box at the top and change the duration to 0.30.
Now the effect will play just once and end, rather than looping instantly. At any time, you can still test the
animation by clicking the Simulate button that floats over the Scene view when the particle effect is selected.
You should have something that looks like Figure 6-2 once you’re done.
Destroying the Particle System
We’re going to put this to one side for a moment now while we write another new script. Create this in the
Scripts folder, as ever, and call it DestroyParticleSystem. Can you guess what it is yet? (Ah good,
a Rolf Harriss reference...)
The script is going to look like this:
using System.Collections.Generic;
using UnityEngine;
public class DestroyParticleEffect : MonoBehaviour {
private ParticleSystem thisParticleSystem;
void Start()
{
thisParticleSystem = GetComponent<ParticleSystem>();
}
void Update()
{
if (thisParticleSystem.isPlaying)
{
return;
}
Destroy(gameObject);
}
}
Figure 6-2. A lovely cloud of blood
Chapter 6 ■ Adding Animations, Effects, and a HUD
102
The purpose of this script is simply to destroy the particle effect once it has finished playing. First we
look for the particular instance of our particle system (which in object-oriented programming is often
referred to as "this") and then we check to see if the particle system is playing during the Update method.
Finally, once the effect has finished playing, we destroy it.
Why is this important? Because otherwise, we would have countless instances of our particle held in
memory, which could eventually start to bog things down. This will make a little more sense in a moment.
For now, just take my word for it, save the script, and add it as a component to the particle effect object you
created earlier.
Rename said particle system as Blood and then drop it into the Prefabs folder. Now delete Blood from
your Hierarchy and your scene.
Making Hazards Hazardous
We’re not quite done yet, though. Next, you need to add additional code to your Hazards script, which will
look like so:
public class Hazards : MonoBehaviour
{
private Player player;
public GameObject Blood;
void Start()
{
player = FindObjectOfType<Player>();
}
void Update()
{
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Instantiate(Blood, player.transform.position, player.transform.rotation);
player.transform.position = new Vector2(-6, 8);
}
}
}
First, we’re looking for a public GameObject that will be referred to as Blood and then we’re
instantiating that GameObject. That means we’re creating an instance of said GameObject, and in this case
we’re using the same coordinates as we have for the player. It’s important that we do this before we move the
player, though.
Make sure you also remember to set the Blood prefab as the GameObject in the Hierarchy for each
of your hazards. You need to do this in the Prefab folder so that it will be reflected across each subsequent
instance of a spike or an enemy (rather than just that particular one). See Figure 6-3.
With all that in place, you can now try playing the game and testing the new effect. Only try watching
the game as it plays in the Scene view rather choosing the full-screen Game view. This way, you should be
able to see what happens: when Squarey walks onto a pit of spikes, he explodes, and an instance of the blood
particle effect is created at that location. You’ll see it appear in the Hierarchy at this point. The effect will then
play itself out before disappearing once the sequence has finished.
And that’s why we needed that DestroyParticleEffect script—otherwise, we would have lots of “finished”
particle effects all in the Hierarchy from all the times we died, and this would take up unnecessary memory.
We can likewise use similar scripts if we create bullets or enemies, so that data is destroyed as we use
it up. In this case, we might have a script that destroys objects after a set amount of time or after a certain
interaction with the player.
Why not try doing something similar for the coins in your game? Create a new particle effect and call it
Sparkle, make sure to add the DestroyParticleEffect script, and then apply it to the coins in the game. Don’t
forget to add the necessary line to the CollectCoin script either. You also want to make the sparkle appear at
the location of the coin, not the location of the player, but I’ll leave you to figure that out.
|