Game Development Reference
In-Depth Information
Step 6. Save and play. We will examine physics in more depth later in
the topic. For now, just accept that this new code makes the capsule
bouncier, and the physics engine will cause it to bounce when
it collides with the plane. PhysicsMaterial in Listing 1.29 is another
type of object. Note the use of the keyword new as it is being
assigned. On this line a new variable called material is being created
and assigned a new PhysicsMaterial . The new keyword creates an
instance of the object. After an instance is created the values
can then be manipulated via referencing the variable to which
it has been assigned—in this case, material .
Step 7. So far we have modified a game object via a script attached
to the camera. Scripts can also be attached directly to a game object.
From the main menu select GameObject > Create Other > Sphere.
A sphere will appear in the Scene. Select the sphere in the Hierarchy
and press the W key. Use the axes in the Scene to move the sphere
above the plane.
Step 8. In the Project, select Create > JavaScript. Rename the script
spherePhysics , attach it to the sphere by dragging and dropping it
from the Project onto the sphere in the Hierarchy, and open it in the
script editor. Add the script in Listing 1.30 .
Listing 1.30 Script to Modify Properties of a Game Object
to Which It Is Attached
var myColor: Color =;
function Start()
var material = new PhysicMaterial();
material.bounciness = 0.5;
this.collider.material = material;
this.renderer.material.color = myColor;
Step 9. Save and play. Now the script is attached to a game object;
it can directly reference the object with the keyword this . There is
no need to create the object because it already exists in the scene.
Because the variable myColor is exposed, you will be able to change it
in the Inspector.
Step 10. From the main menu select GameObject > Create Other >
Cube. A cube will appear in the scene. Position it above the plane
and to one side of the sphere. Attach the spherePhysics script to
the new cube.
Search Nedrilad ::

Custom Search