Objective: Create a script to guide our missile to the nearest enemy.
Welcome to the first part of this series. In this article, we are going to create some requirements for the functionality of our homing missile. So let's get started.
Objective: Write C# code to control the left and right side thruster animations.
In part two of this series, we added thruster animations to our spaceship, the main thruster on the rear of the spaceship, and two side thrusters. The main thruster on the rear of the spaceship is always active. The two side thrusters are active only when the ship is turning. To do this we will have to write a few lines of code to our “Player” C# script.
Objective: Add thruster animations to our main engine and side thrusters for left and right movement.
In part one we added some C# code to allow us to move our space up, down, left, and right. Now we need to add some animations to provide the thruster visual effects.
Next, we create an animation.
Objective: Increase “Player Speed when the “Left Shift” key is held down.
To make this magic is the use of the “Input.GetKey” function call. If we use the “Input.GetKeyDown” we only get a TRUE condition for a very brief moment after the key is pressed. “Input.GetKey” stays TRUE as long as we hold the key down.
While holding down the “Left Shift” key we change the value of “_speed” from 4 to 8.
Objective: Provide functionality to quit a Unity application using the “Escape” (Esc) key.
This is a simple task to complete thanks to the “Application.Quit( )” function. Place the above code in a “void Update( )” function body as shown below and you are finished.
Ok, you worked hard on putting your game together and now you want to be able to create a “Build” or generate the file(s) needed to run the game outside of the Unity editor. This can be a simple task for a small game. So let's “build” our game.
To get started click on “Build Settings”.
Create an “Empty Game Object” and call it “Audio_Manager”. Use Add Component” to add the “Audio Source” component. Create a C# script and name it “ Play Laser Sound”. Add the C# script to the “Audio Manager” game object (“Drag and Drop” on to the “Audio_Manager”).
Open the “ Play Laser Sound” C# script and create a private Serialized Field variable of type AudioClip and call it “_laserSoundClip”.
Create a second private variable of type “AudioSource” and call it “_audioSource”