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:
Rigidbody example
.0
(all axes), rotation 0
(all axes), and scale 20
, 1
, 20
for the x, y, and z axes respectively:In the preceding screenshot, we have set the Is Trigger property in the blue cube.
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.
RigidbodyController.js
. Add it to the Red Cube game object, as shown in the following screenshot: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.
3.14.245.167