We need to create a prefab of our EnergyPulse projectile. Use the following screenshot of the Inspector as a guide for these steps.
EnergyPulse
.0
, 0
, 0
.0.5
, 0.5
, 0.5
.EnergyPulsePower
(see Appendix B).Now that we have our EnergyPulse to shoot, we can shoot them in two States: PlayStateScene1_2
and PlayStateScene2
.
PlayStateScene1_2
only allows a single shot to be fired for each press of the key.
An analysis of the code we saw in the preceding code screenshot is as follows:
Line 44: if(Input.GetKeyDown(KeyCode.LeftControl))
GetKeyDown()
method means one shot will be fired every time the left Ctrl key is pressedLine 46: controller.FireEnergyPulse();
FireEnergyPulse()
in PlayerControl
, line 56, is called to fire an EnergyPulse
PlayStateScene2
allows for continuous firing by holding the Left Ctrl key down.
An analysis of the code we saw in the preceding code screenshot is as follows:
Line 47: if(Input.GetKey(KeyCode.LeftControl))
GetKey()
method means shots will be fired continuously when the left Ctrl key is pressed downLine 49: controller.FireEnergyPulse();
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:
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 instantiatedLine 56: public void FireEnergyPulse()
PlayStateScene1_2
, line 46, or PlayStateScene2
, line 49Line 58: Rigidbody clone;
clone
stores a reference to the new Rigidbody
object about to be createdclone
is of type Rigidbody
Line 59: clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
Instantiate()
method takes the Prefab stored in the variable projectile
and creates an EnergyPulse GameObjectclone
stores a Rigidbody
type of object, and EnergyPulse is of type GameObject
, EnergyPulse has to be converted to a Rigidbody
typeas
operator will cast (convert) the GameObject to a RigidbodyLine 60: clone.transform.Translate(0, .5f, 2.1f);
Translate()
moves it in front of Player before the frame is ready to displayLine 61: clone.velocity = transform.TransformDirection(Vector3.forward * 50);
force
of Rigidbody
being applied to move EnergyPulse forwardWe now have our EnergyPulse being fired. Now we need to make things happen when it hits something.
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:
Anything else they hit is immune to the effects of the EnergyPulse. The following screenshot shows how this is done:
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;
Line 12: pulseDuration -= Time.deltaTime;
1.0
per secondLine 14: if(pulseDuration <= 0)
pulseDuration
reaches 0
, line 15 is executedLine 15: Destroy(gameObject);
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.
Line 8: public Transform goodOrb;
goodOrb
variable stores the GoodOrb prefab so it can be instantiatedLine 18: void OnTriggerEnter(Collider other)
other
variable now stores a reference to the Collider of the GameObject that was hitLine 20: if(other.gameObject.tag == "BadOrb")
BadOrb
, then the code block is executedBadOrb
, then line 28 is executedLine 22: Instantiate(goodOrb, other.transform.position, other.transform.rotation);
Line 23: GameObject.Find("GameManager"). GetComponent<GameData>().playerLives += 1;
playerLives
in GameData
and adds 1 to it, increasing Player LivesLine 24: Destroy(other.gameObject);
Line 25: Destroy(ga
meObject);
Line 28: Destroy(gameObject);
BadOrb
GameObject
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.
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?
3.129.70.185