In this section, we will continue creating the rocket prefab, which will shoot out from the launcher. We will also create the trail smoke the follows this rocket, the smoke from the launcher barrel, and the explosion when the rocket hits something.
We will start with creating the SmokePosition, which is the position of the smoke particle when the character fires the rocket.
Now, we got the smoke position from the launcher barrel.
smoke
prefab object by going to the Project view, and click on the folder in this order: Standard Assets | Particles | Smoke, we will see Fluffy Smoke
prefab, drag it to the Hierarchy view. Then, we will go to its Inspector view and start changing the parameters as follows:Smoke
script by going to Assets | Create | Javascript and name it Smoke
. Then, we go to the script and type the following code:public var timeOut : float = 0.5; // Destroy after 0.5 seconds. // Use this for initialization public function Start () : void { Invoke("KillObject", timeOut); } public function KillObject () : void { //Stop the emit the particle var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter); if (emitter != null) { emitter.emit = false; // Stop Emit } //In here We set the particle to auto destruct to destroy itself after a life time (or we can setup it up in the editor) var particleAnimator : ParticleAnimator = GetComponentInChildren(ParticleAnimator); if (particleAnimator != null) { particleAnimator.autodestruct = true; } }
This Smoke
script will be added to the Fluffy Smoke
object that we just created. In this function, we use the Invoke()
function, which will tell the script to call the KillObject()
function after the timeout
(0.5 seconds).
Smoke
script which we created to this Fluffy Smoke
object (this will break the prefab again, so click on continue to break the prefab).Fluffy Smoke
object by going to Assets | Create | Prefab and name it ShotSmoke
. Then, drag the Fluffy Smoke
object in the Hierarchy view to the ShotSmoke
prefab in the Project view. Finally, we remove the Fluffy Smoke
object in the Hierarchy view by right-clicking on the Fluffy Smoke
object in the Hierarchy view and choosing Delete; now we have the new ShotSmoke
prefab.rocket
model and go to its Inspector view to remove the Animation by right-clicking and choosing Remove Component. (This will bring up the losing prefab pop-up, so we just click Continue to break the prefab.)Rocket
script, so go to Assets | Create | Javascript and name it Rocket
; double-click on it to open MonoDevelop. Then, go to the script and replace the code as follows:@script RequireComponent(ConstantForce) public var timeOut : float = 3.0; // Destroy after 3.0 seconds. public var explosionParticle : GameObject; // Use this for initialization public function Start () : void { Invoke("KillObject", timeOut); } public function OnCollisionEnter (others : Collision) : void { //Create the explosion on the first impact point of the rocket and collider var contactPoint : ContactPoint = others.contacts[0]; var rotation : Quaternion = Quaternion.FromToRotation(Vector3.up, contactPoint.normal); GameObject.Instantiate(explosionParticle, contactPoint.point, rotation); KillObject(); } public function KillObject () : void { //Stop the emit the particle var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter); if (emitter != null) { emitter.emit = false; // Stop Emit } //In here We set the particle to auto destruct to destroy itself after a life time (or we can setup it in the editor) var particleAnimator : ParticleAnimator = GetComponentInChildren(ParticleAnimator); if (particleAnimator != null) { particleAnimator.autodestruct = true; } //Detach the trail renderer in our particles transform.DetachChildren(); //Destroy this Object GameObject.Destroy(gameObject); }
Rocket
script that we created by dragging the script to the rocket
object in the Hierarchy.rocket
object by going to Component | Physics | Box Collider.Smoke Trail
prefab as a child of this rocket
object. Go to the Project view, and click on Standard Assets | Particles | Smoke and drag the Smoke Trail
prefab to the rocket
object in the Hierarchy view.Smoke Trail
object in the Hierarchy view and set the following in its Inspector view:rocket
object. Go to Assets | Create | Prefab and name it Rocket
, and then drag our rocket
object in the Hierarchy view to Rocket
prefab, which we just created in the Project view. Finally, we remove the rocket
object from the Hierarchy view by deleting it, and now we have the new Rocket
prefab.RocketLauncher
script by going to the Project view, double-click on the RocketLauncher
script to go to MonoDevelop, and add the following new script at the beginning:public var smoke : GameObject; public var smokePosition : Transform; public var rocket : ConstantForce; public var speed : float = 10; public var ammoCount : int = 20; private var lastShot : float = 0.0;
Fire()
function and add the following highlighted code:public function Fire(_reloadTime : float) : void { if (Time.time > (_reloadTime + lastShot) && ammoCount > 0) { var rocketPrefab : ConstantForce = ConstantForce.Instantiate(rocket, transform.position, transform.rotation); rocketPrefab.relativeForce = new Vector3(0, 0, speed); var smoke : GameObject = GameObject.Instantiate(smoke, smokePosition.position, smokePosition.rotation); //We ignore the collision between rocket and character Physics.IgnoreCollision(rocketPrefab.collider, transform.root.collider); //Get the last shot time lastShot = Time.time; //Decrease the bullet ammoCount--; } }
First Person Controller
object in the Hierarchy view. Then, go down two more steps inside this object until we see the RocketLauncher
object, as shown in the following screenshot:RocketLauncher
object to bring up its Inspector view. Then set the following:Now, we can click Play to see the result. We should be able to walk around by pressing the arrow, W, A, S, D, or Space key to jump, move the mouse to rotate around, press E to aim, press R to reload the rocket, and click on the left mouse button to fire the rocket. However, we won't be able to see the number of bullets right now, because we don't have any UI set up to show the number yet. So, in the next step, we will create a bullet count
UI by using GUITexture and OnGUI()
.
In this section, we just created the rocket and particle effect that will appear when the player presses fire. It seems like a lot to do in one section, but it was worth it.
First, in the Rocket
script, we used @script RequireComponent(ConstantForce)
to tell the script to require the ConstantForce for this rocket; this will tell Unity to basically add the ConstantForce automatically when we add this script to the object.
ConstantForce is one of the Physics components in Unity that will add a constant force to the RigidBody object (the ConstantForce works with the RigidBody components, so when we add the ConstantForce to our object, Unity will automatically add the RigidBody as well), which will contain the properties that we can use to control the rocket movement. For more details please have a look at the following website:
http://unity3d.com/support/documentation/Components/class-ConstantForce.html.
Next, we have the timeout
and explosion
parameters. We then have the Invoke()
function calling the KillObject()
function after the timeout
(3.0 seconds). Then, we check for the collision object—if the rocket hits something, it will get to the position where the collision occurs, add the explosion object to that position, and then the rocket object will kill itself.
The KillObject()
function basically stops the particle emitter, makes sure that the particle will destroy itself by setting the autodestruct
parameter to true
, detaches the particle, and destroys the game object itself.
Now, we create the script for the rocket and smoke particle. Then, we use the built-in particle package and adapt to the way we want for smoke and smoke trial. Finally, we put everything together and get the result as expected.
In this section, we used the Instantiate()
function to clone a new game object from the prefab object in the Project view. The Instantiate()
function takes three parameters, which are the original Object, Position (Vector3), and Rotation (Quaternion). The Position and Rotation objects are the transform parameters at the start position of the object, which will be created in the scene. The Instantiate()
function can take any kind of object and the result can also be returned to any kind of objects. We can also see more examples and details from the Unity document at:
http://unity3d.com/support/documentation/ScriptReference/Object.Instantiate.html
http://unity3d.com/support/documentation/Manual/Instantiating%20Prefabs.html.
Next, we will talk about the Invoke()
function, which we used to call the function after the time we have set in seconds. If some of you have experience with Actionscript, this function is very similar to the setTimeOut()
function. We can also use InvokeRepeating()
to call the method similar to the Invoke()
function, but this function will repeat calling that function every time we set in seconds. We can see more details about the Invoke()
function from the Unity document at:
http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.Invoke.html.
For the InvokeRepeating()
function, refer to the following website:
http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.InvokeRepeating.html.
Finally, let's talk about the position of the rocket that launches from the same position of the scope target UI. This is a bit tricky because we want the player to be able to aim and shoot the rocket exactly to the same position that he/she is aiming at. So, we add the RocketLauncher
object as a child of the Main Camera
because we want it to move or rotate with the user view, which is the Main Camera
. Then, we add the scope target UI at the center of the screen because it is easier for the player to aim than than if we put it on one side. So, we set up the positions X
and Y
of RocketLauncher
object to 0
because it is the same position to the Main Camera
and if we get the position of the Main Camera
to the screen position it will be the center of the screen. Then, we set up the Z
position of RocketLauncher
object to 2
because it is the same depth as our character weapon graphics. This is to make the rocket not too close to the camera. We can also see it from the following diagram:
3.144.97.216