Game Development Reference
In-Depth Information
Step 15. Now that the player can pick up a key, we want to make it
possible for the key to open the red door. To do this, create a new
JavaScript file called opendoor and open it in the script editor. Add
the code shown in Listing 6.7 .
Listing 6.7 Script to Slide a Game Object in the x Direction
var open = false;
function Update ()
{
if(open)
{
this.transform.position.x+=Time.time * 0.01;
if(this.transform.position.x > 21.8)
open = false;
}
}
Step 16. Attach this script to the door object and then add the tag
door.
Step 17. The script will activate when the open variable is set to true.
This will cause the door to slide along its x axis. The open variable
needs to be set to true when the 3rd Person Character walks into the
door while holding the RedKey object. To make this happen, modify
the pickup script to that in Listing 6.8 .
Listing 6.8 Script to Activate a Game Object's Code When
Collision Conditions Are Met
var attachPoint: GameObject;
private var currentObj: GameObject;
function Start()
{
//first held object
currentObj = GameObject.Find("wrench");
}
function OnControllerColliderHit (hit : ControllerColliderHit)
{
if(hit.gameObject.tag == "door")
{
if(currentObj.gameObject.name == "RedKey")
hit.gameObject.GetComponent ("opendoor").
open =
true;
}
else if(hit.gameObject.tag == "key")
Search Nedrilad ::




Custom Search