Time for action – creating the EnergyPulse prefab

We need to create a prefab of our EnergyPulse projectile. Use the following screenshot of the Inspector as a guide for these steps.

  1. Create a new prefab by navigating to Assets | Create | Prefab and name it EnergyPulse.
  2. Add a Sphere to the Scene.
  3. Position its Transform to 0, 0, 0.
  4. Scale its Transform to 0.5, 0.5, 0.5.
  5. On the Sphere Collider, check Is Trigger.
  6. Uncheck Mesh Renderer.
  7. Add a Rigidbody Component.
  8. Turn off Gravity.
  9. Add the script EnergyPulsePower (see Appendix B).
  10. Add a Trail Renderer Component by navigating toComponent | Effects | Trail Renderer.
  11. Drag a GoodOrb prefab to the Good Orb property on the Energy Pulse Power (Script).
  12. Set the Trail Renderer properties as shown in the following screenshot.
  13. Drag Sphere onto the newly created, empty EnergyPulse prefab.
  14. Now delete Sphere from Scene
    Time for action – creating the EnergyPulse prefab

What just happened?

Now that we have our EnergyPulse to shoot, we can shoot them in two States: PlayStateScene1_2 and PlayStateScene2.

Shooting a single-shot EnergyPulse

PlayStateScene1_2 only allows a single shot to be fired for each press of the key.

Shooting a single-shot EnergyPulse

An analysis of the code we saw in the preceding code screenshot is as follows:

Line 44: if(Input.GetKeyDown(KeyCode.LeftControl))

  • The GetKeyDown() method means one shot will be fired every time the left Ctrl key is pressed

Line 46: controller.FireEnergyPulse();

  • FireEnergyPulse() in PlayerControl, line 56, is called to fire an EnergyPulse

Shooting rapid-fire EnergyPulses

PlayStateScene2 allows for continuous firing by holding the Left Ctrl key down.

Shooting rapid-fire EnergyPulses

An analysis of the code we saw in the preceding code screenshot is as follows:

Line 47: if(Input.GetKey(KeyCode.LeftControl))

  • The GetKey() method means shots will be fired continuously when the left Ctrl key is pressed down

Line 49: controller.FireEnergyPulse();

  • The FireEnergyPulse() method in PlayerControl, line 56, is called to fire an EnergyPulse

The EnergyPulse is fired

In PlayerControl, the EnergyPulse is instantiated in front of Player, and force is used to propel it forward. The EnergyPulse has to be instantiated in front of Player so that the EnergyPulse Collider doesn't detect the Player Collider immediately after being instantiated. If it does detect it, the EnergyPulse will be destroyed immediately.

The PlayerControl script needs to know about the EnergyPulse prefab so that it can be created in code. Drag the EnergyPulse prefab to the Projectile variable property in the Inspector:

The EnergyPulse is fired
The EnergyPulse is fired

An analysis of the code we saw in the preceding code screenshot is as follows:

Line 10: public Rigidbody proj ectile;

  • projectile stores the EnergyPulse prefab so it can be instantiated

Line 56: public void FireEnergyPulse()

  • This is called by PlayStateScene1_2, line 46, or PlayStateScene2, line 49

Line 58: Rigidbody clone;

  • clone stores a reference to the new Rigidbody object about to be created
  • Since we will be using physics force to propel the EnergyPulse forward, the variable clone is of type Rigidbody

Line 59: clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;

  • The Instantiate() method takes the Prefab stored in the variable projectile and creates an EnergyPulse GameObject
  • The method also places in it at the same position as Player but we'll fix this
  • Since the variable clone stores a Rigidbody type of object, and EnergyPulse is of type GameObject, EnergyPulse has to be converted to a Rigidbody type
  • The as operator will cast (convert) the GameObject to a Rigidbody

Line 60: clone.transform.Translate(0, .5f, 2.1f);

  • Since we can't have the EnergyPulse object at the same position as Player, Translate()moves it in front of Player before the frame is ready to display

Line 61: clone.velocity = transform.TransformDirection(Vector3.forward * 50);

  • This sets the force of Rigidbody being applied to move EnergyPulse forward

We now have our EnergyPulse being fired. Now we need to make things happen when it hits something.

Controlling EnergyPulse objects

So Player shoots out this EnergyPulse, what happens next? Does it just go on forever? What happens if it hits something?

We certainly don't want these instantiated EnergyPulse Gameobjects hanging around forever. Once they've served their purpose, they should be destroyed. That's exactly what we do. After a set time, they simply disappear.

The EnergyPulse is meant to do two things:

  • Increase Player Lives
  • Convert a BadOrb into a GoodOrb

Anything else they hit is immune to the effects of the EnergyPulse. The following screenshot shows how this is done:

Controlling EnergyPulse objects

An analysis of the code we saw in the preceding code screenshot is as follows:

The EnergyPulsePower script is attached to the EnergyPulse GameObject.

Line 6: public float pulseDuration = 1f;

  • This sets how long an EnergyPulse will exist
  • This time is adjustable in the Inspector

Line 12: pulseDuration -= Time.deltaTime;

  • This is a countdown timer that operates at the rate of 1.0 per second

Line 14: if(pulseDuration <= 0)

  • When the value in pulseDuration reaches 0, line 15 is executed

Line 15: Destroy(gameObject);

  • Since this script is attached to the EnergyPulse GameObject, this EnergyPulse is destroyed when the timer reaches 0

That makes sure an EnergyPulse does not last forever.

Now we look into increasing Player Lives and converting BadOrbs into GoodOrbs. What happens is that a BadOrb collides with an EnergyPulse. The BadOrb is destroyed and disappears. A new GoodOrb is instantiated and placed exactly where the BadOrb used to be, giving the appearance that it was converted.

Controlling EnergyPulse objects

Line 8: public Transform goodOrb;

  • The goodOrb variable stores the GoodOrb prefab so it can be instantiated

Line 18: void OnTriggerEnter(Collider other)

  • This method is called by Unity when the Sphere Collider of the EnergyPulse hits any GameObject with a Collider
  • The other variable now stores a reference to the Collider of the GameObject that was hit

Line 20: if(other.gameObject.tag == "BadOrb")

  • If the hit GameObject's tag is BadOrb, then the code block is executed
  • If it does not have a tag of BadOrb, then line 28 is executed

Line 22: Instantiate(goodOrb, other.transform.position, other.transform.rotation);

  • GoodOrb is instantiated and placed right where BadOrb is located

Line 23: GameObject.Find("GameManager"). GetComponent<GameData>().playerLives += 1;

  • This finds the variable playerLives in GameData and adds 1 to it, increasing Player Lives

Line 24: Destroy(other.gameObject);

  • This destroys the BadOrb gameObject

Line 25: Destroy(ga meObject);

  • EnergyPulse hit something, so it is destroyed and disappears

Line 28: Destroy(gameObject);

  • When EnergyPulse hits something it is destroyed and disappears
  • This is executed when EnergyPulse hits anything other than a BadOrb GameObject

Have a go hero – analyzing the code for the second level of play

Scene2 is very similar to Scene1. It's included to show the ability to have a game with more than one level of play. Switching to the PlayStateScene2 State is just as easy as switching to any other State. I did not explain the code for Scene2 because it would be redundant analysis. You already know all the fundamentals and concepts to be able to look at the code and know how it works. The challenge for you is to analyze the classes and code for Scene2, and in the process realize that you now have the ability to do just that, read and understand code.

Pop quiz – knowing which Unity methods to call

Q1. Using code, what are the two ways to move a GameObject in a game?

Q2. When applying a force to a GameObject to move it, what method should be used? (Hint: it's not Update()).

Q3. When wanting to fire a projectile that doesn't even exist in the scene, how do you get one to appear in the game?

Q4. To display buttons and text on the game screen, what Unity method is used?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.250.203