Game Development Reference
In-Depth Information
Step 16. Save this code and attach it to the cannonball prefab.
Step 17. In the Standard Assets > Detonator > Prefab Examples folder
in the Project, select a detonator prefab and drag and drop it onto the
Explosion variable of the explode script attached to the cannonball.
Step 18. Play. The cannonballs will now explode when they impact.
Step 19. To allow the player to set the strength of the cannonball shot,
we will add a meter. The shot strength will build up while the space
is held down and the cannonball will be released when the space is
released. Create a new JavaScript file called strengthMeter.js and add
the code in Listing 4.18 .
Listing 4.18 Creating a Visual Meter for Setting Cannon
var shotStrength : float;
function OnGUI () {
GUI.Box (Rect (0,0,220,40), "Force");
shotStrength = Slider (Rect (10,20,200,30), shotStrength);
function Slider (screenRect : Rect, strength : float) : float
strength = GUI.HorizontalSlider (screenRect,
strength, 0.0, 1.0);
return strength;
Step 20. Attach this new script to the Main Camera.
Step 21. Edit aim.js to reflect the changes shown in Listing 4.19 .
Listing 4.19 Script to Build up Firing Strength When Space
Is Down and to Fire When Space Is Released
var cannonball:GameObject;
function Update ()
if (Input.GetKey ("left"))
this.transform.RotateAround (this.transform.
this.transform.forward, 20 * Time.
Search Nedrilad ::

Custom Search