Game Development Reference
In-Depth Information
Select the Cube game object and change its Transform position to (1, 1, 0), rotation to (350, 0, 0) and
scale to (1, 1, 3). In the Hierarchy view, create a Capsule game object and set its Transform position
to (0, 1, 0). Again in Hierarchy, drag Cube and drop it on the Capsule game object to make it a child
object. Notice that the Transform of the Cube has changed; now its position is designated as relative
to its parent object, the Capsule, rather than relative to the origin of world space. The Capsule game
object represents the player and the Cube game object represents the rocket launcher.
The Sphere game object will represent the rockets being fired. Select the Sphere and delete the
Forces script component by clicking the cog icon and Remove Component. Then in the Constant
Force component, change the Sphere's Relative Force z coordinate to 35, then its Transform.
position to (1, 1, 2). In the Hierarchy view, drag the Sphere onto the Cube to make it a child object of
the Cube so it will always launch from the end of the Cube rocket launcher whichever way the player
is pointing it.
You'll want to fire more than one rocket, so you need to make the Sphere into a prefab. You'll do
this the same way you created the snazzy cube prefab in Chapter 3. Create a Prefabs folder in
the Project panel. Drag the Sphere from the Hierarchy to the Prefabs folder, then name the prefab
Rocket. Delete the Sphere from Hierarchy view by right-clicking and selecting Delete.
In the Project panel, open the Scripts folder, then select Create ➤ Javascript and name the new
script RocketLaunch. Double-click the script icon to open it in MonoDevelop. Edit the code to the
following:
#pragma strict
public var rocket : GameObject;
function Update () {
if(Input.GetButtonUp("Fire1")) {
var rocketInstance : GameObject = Instantiate(rocket);
}
}
All of this should be familiar from previous chapters. First you declare a GameObject type reference
variable named rocket . Then, in the Update() function you use the if statement to check for the
condition where the default “Fire1” button managed by the Input Manager has been released from
being pressed. If this condition is true , then a new instance of the rocket is created. Save the script.
In the editor, drag the RocketLaunch script from the Project panel and drop it onto the Main Camera
in the Hierarchy. With the Main Camera selected, you'll see the RocketLaunch script has been added
as a component. The Rocket field is empty, so if you were to play now, nothing would happen when
you try to launch a rocket. Drag the Sphere prefab from the Prefabs folder in the Project panel and
drop it into the Rocket field in the Inspector. Save the scene.
Playtest using the left Ctrl key, which is the default Fire1 button. Yay! You are launching spheres that
are accelerating like a rocket. The Drag property of the rigidbody will oppose any of these means of
acceleration and can be used to limit the maximum velocity.
Several methods of accomplishing an acceleration effect are presented to you here. Which to use
always depends on the context within your game. The important thing here is that Unity often
has more than one solution, one of which may be more appropriate for your game context and
 
Search WWH ::




Custom Search