Game Development Reference
In-Depth Information
This breaks down as follows:
(1) public var player : GameObject;
Declare a public variable
player
of type GameObject.
(2) player = GameObject.FindWithTag("Player");
When the
Start()
function is called, find the reference to the player character by its Player tag and
assign it to
player
.
(3) if(Input.GetKeyDown(KeyCode.Alpha1)) {
With each frame
Update()
call, check to see if the player has pressed the 1 key on the alphanumeric
keyboard. If so, execute the subsequent block of code.
(4) if (player != null) {
Test for the presence of a valid GameObject reference. If not
null
, execute the block of code.
(5) player.transform.position = Vector3(0, 9, 18);
Move the player to the top of the ramp by resetting its Transform position coordinates.
(6) player.gameObject.GetComponent(GoRagdoll).ExitRagdoll();
If the player character was killed, it needs to be reset out of ragdoll mode for the player to continue.
You will create the
ExitRagdoll()
function next.
In the Unity editor top menu, select Game Object ➤ Create Empty. In the Hierarchy select the new
game object and rename it Cheats. The sole purpose of this game object is to hold the cheat code
scripts. Change its Transform position to (0, 20, 0) just to get it out of the way. Now attach the
Cheats script either by dragging the script onto the Cheats game object in the Scene view or in the
Inspector with Add Component ➤ Scripts and select the Cheats script from menu.
You still need to create the
ExitRagdoll()
function for the player character. In the Project panel
Assets ➤ Scripts folder, select the GoRagdoll script and open it in MonoDevelop. This function
simply reverses everything that occurred in the
GotoRagdoll()
function created in Chapter 7. First,
select the
GotoRagdoll()
function and copy it. After the close bracket of the
GotoRagdoll()
function,
paste the copied code back into the script, then change the copied function name to ExitRagdoll.
Change all the boolean flags to their alternate settings and your
ExitRagdoll()
function should now
read as follows:
function ExitRagdoll () {
if (childRigidBodies != null) {
for (var childRigidBody : Rigidbody in childRigidBodies)
{
childRigidBody.isKinematic = true;
}
}
if (childColliders != null) {
for (var childCollider : Collider in childColliders)