Game Development Reference
In-Depth Information
Step 4. One of the functions available to the Game Object is
AddComponent() . This script performs the same task as selecting the
Game Object in the Editor and choosing an item from Component
in the main menu. To add a physics component to the Game Object,
modify your code to that in Listing 1.28 . Save and play.
Listing 1.28 Adding a Rigidbody to a Game Object
Using Script
private var gameObj : GameObject;
function Start()
{
gameObj = GameObject.CreatePrimitive(PrimitiveType.Capsule);
gameObj.transform.position.x = 1;
gameObj.transform.position.y = 5;
gameObj.transform.position.z = 2;
gameObj.AddComponent("Rigidbody");
}
Step 5. The Rigidbody component is used by the physics system. As
you play your new script, the capsule will fall due to gravity until it
hits the plane in the scene. The collision event will cause the capsule
to stop dead in its tracks. If you examine the capsule in the Inspector
while the script is playing, you will notice that the capsule has a
Collider component. This is used by the physics system to determine
the boundary area of the object in order to calculate collisions. You
can access the collider and change its values in the script. Modify
your script to that in Listing 1.29 .
Listing 1.29 Modifying the Physics Material of a Game Object
private var gameObj : GameObject;
function Start()
{
gameObj = GameObject.
CreatePrimitive(PrimitiveType.Capsule);
gameObj.transform.position.x = 1;
gameObj.transform.position.y = 5;
gameObj.transform.position.z = 2;
gameObj.AddComponent("Rigidbody");
var material = new PhysicMaterial();
material.bounciness = 1;
gameObj.collider.material = material;
}
Search Nedrilad ::




Custom Search