Example using a Rigidbody

Let's assume that in our game, we require a cube to be our character. In this case, we won't be using the character controller. Let's follow these steps:

  1. Create a new scene and save it as Rigidbody example.
  2. As shown in the following screenshot, create a Plane game object and set its properties to position 0 (all axes), rotation 0 (all axes), and scale 20, 1, 20 for the x, y, and z axes respectively:
    Example using a Rigidbody
  3. Add a Cube game object and a Rigidbody Collider. Place it above the plane.
  4. As shown in the following screenshot, add material to the red cube:
    Example using a Rigidbody
  5. Create another Cube game object. I have used a white cube without a Rigidbody.
  6. Create another Cube object without a Rigidbody. I have used a blue cube, as shown in the following screenshot, and have set the Is Trigger property to true:
    Example using a Rigidbody

    In the preceding screenshot, we have set the Is Trigger property in the blue cube.

  7. Create a new script and save it as TriggerController.js; add it to the Trigger Collider game object and put the following code inside it:
    functionOnTriggerEnter( other : Collider )
    {
      Debug.Log("OnTrigger Event");
    }
    
    functionOnTriggerExit( other : Collider )
    {
      Debug.Log("OnTriggerExit Event");
    }

    The preceding code will show the logon events in the console window: OnTriggerEnter and OnTriggerExit.

  8. Let's move to the next step. Create a new script and save it as RigidbodyController.js. Add it to the Red Cube game object, as shown in the following screenshot:
    Example using a Rigidbody

Add the following code to the RigidbodyController.js script file:

#pragma strict
@scriptRequireComponent(Rigidbody) 
/******* RequirementComponent ensure there should be a Rigid body as it asks for Rigidbody as required component/ 
privatevaronPlane = false; 
// Multiply the velocity when using the velocity.
varvelocityMultiplier = 100.0f;
// Multiply the move distance.
varmoveMultiplier= 8.0f;
// Multiply the force to make the cube jump.
varjumpMultiplier= 700.0f;


functionFixedUpdate(){
// Calculate the velocity/move direction based on the user input.
  varmoveDirection = Vector3( Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical") ) * Time.deltaTime;
moveDirection = Camera.main.transform.TransformDirection( moveDirection );
// Here TransformDirection transforms direction from local space
// to world space.

  if ( rigidbody != null && !rigidbody.isKinematic )
    {
    rigidbody.MovePosition( rigidbody.position + ( moveDirection * moveMultiplier ) );
rigidbody.velocity = Vector3( 0, rigidbody.velocity.y, 0 );

  }

if ( Input.GetButtonDown("Jump") &&onPlane )
  {

    rigidbody.AddForce( Vector3.up * jumpMultiplier );
     }

}



functionOnCollisionStay(collisionInfo : Collision)
{
  onPlane = true;
}


functionOnCollisionExit( collisionInfo : Collision )
{
onPlane = false;

}

Run the project and you will see the Rigidbody actions on the red cube by clicking on the Jump button.

In the preceding example, we have learned how to apply actions on a Rigidbody using scripts.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.218.45